Ruby on rails Rails 4自动生成的用于删除的RSpec测试失败,即使该功能正在开发中

Ruby on rails Rails 4自动生成的用于删除的RSpec测试失败,即使该功能正在开发中,ruby-on-rails,rspec,ruby-on-rails-4,Ruby On Rails,Rspec,Ruby On Rails 4,这是Rails 4中销毁操作的自动生成测试,作为车辆\u控制器.rb规范的一部分: describe "DELETE destroy" do it "destroys the requested vehicle" do vehicle = Vehicle.create! valid_attributes expect { delete :destroy, {:id => vehicle.to_param}, valid_sessi

这是Rails 4中销毁操作的自动生成测试,作为
车辆\u控制器.rb规范的一部分:

describe "DELETE destroy" do
     it "destroys the requested vehicle" do
       vehicle = Vehicle.create! valid_attributes
        expect {
          delete :destroy, {:id => vehicle.to_param}, valid_session
        }.to change(Vehicle, :count).by(-1)
     end

    it "redirects to the vehicles list" do
      vehicle = Vehicle.create! valid_attributes
      delete :destroy, {:id => vehicle.to_param}, valid_session
      response.should redirect_to(vehicles_url)
    end
end
这是我在控制器中得到的,同样非常标准:

def destroy
  @vehicle = Vehicle.find(params[:id])
  @vehicle.destroy
  flash[:notice] = "Vehicle has been deleted"
  redirect_to vehicles_url
end
这在应用程序本身中运行得很好——当你删除一辆车时,它会重定向回vehicles_url,并且条目会从数据库中删除。服务器日志看起来也完全正常。但是,当我运行规范时,它们失败如下:

1) VehiclesController DELETE destroy destroys the requested vehicle
   Failure/Error: expect {
     count should have been changed by -1, but was changed by 0
   # ./spec/controllers/vehicles_controller_spec.rb:148:in `block (3 levels) in <top (required)>'

2) VehiclesController DELETE destroy redirects to the vehicles list
   Failure/Error: response.should redirect_to(vehicles_url)
     Expected response to be a redirect to <http://test.host/vehicles> but was a redirect to <http://test.host/>.
     Expected "http://test.host/vehicles" to be === "http://test.host/".
   # ./spec/controllers/vehicles_controller_spec.rb:156:in `block (3 levels) in <top (required)>'
现在,如果我尝试运行规范,我会收到以下语法错误:

/home/kathryn/testing/spec/controllers/vehicles\u controller\u spec.rb:150:语法错误,意外'\n',预期=>(语法错误)


第150行是指第一个规范中以
delete:destroy
开头的行,在该行中进行了更改。

将参数传递到
delete
时遇到了麻烦。请参见中的方法定义

您可以使用:

delete :destroy, {id: vehicle.to_param, session: valid_session}


我不太清楚您传入的最后两个参数在
ActionController
中发生了什么,但它不可能是正确的。

Ruby的解析器与内联哈希混淆了。您可能想要:

    delete :destroy, {:id => vehicle.to_param}, valid_session

您很可能遇到授权错误。尝试在任一测试中的
delete
操作后添加
flash[:alert]。应==nil
。如果您遇到授权问题,您将在测试失败中遇到类似“您无权访问此页面”的问题

关于这一点的最大线索是,您的测试也在以下方面失败:

然后(这很重要!)删除所有
、有效的会话
参数,否则它们将覆盖Desive使用
登录
方法为您设置的会话

这是最简单的形式;如果某些用户可以(例如)删除,而其他用户不能删除,则此操作仍将失败,因此对于这些情况,您可能需要创建一个单独的
描述“管理员用户可以”执行的操作
<代码>结束
测试块,并在(:each)之前有一个
,它调用
与管理员用户登录


我希望这会有帮助,祝你好运

我已经迁移了我的测试数据库,因为我还有大约20个其他规范正在通过。我将在原始帖子中添加一条关于before过滤器的注释。此人的情况似乎与我的问题完全相关,但不幸的是,当我将其从哈希中删除时,我得到了错误
/home/kathryn/testing/spec/controllers/vehicles\u controller\u spec.rb:150:语法错误,意外的'\n',expecting=>(SyntaxError)
。谢谢你的链接。你能发布你的新代码吗?语法错误应该很容易修复。我将把新代码添加到我的原始文章的底部。我所做的就是去掉花括号,这样它就不再是ruby哈希了。也许这还有其他我没有预料到的语法后果。谢谢你看!好的,但是如果您不使用Desive并且没有可用的登录功能,您将如何执行此操作?如何使用有效的会话方法?我找不到这样的例子。
delete :destroy, id: vehicle:to_param, session: valid_session
    delete :destroy, {:id => vehicle.to_param}, valid_session
Expected response to be a redirect to <http://test.host/vehicles> but was a
redirect to <http://test.host/>.
describe VehiclesController do
  include Devise::TestHelpers

  before(:each) do
    sign_in FactoryGirl.create(:user)
  end

  . . .