我刚刚将我的应用程序从Rails 3.2升级到Rails 4.
我正在完成需要进行的各种更改,但这一次让我很难过。
我的销毁行动似乎在应用程序中有效,但是当我运行我的rspec测试时,他们失败了。
这是我的控制器中似乎抛出错误的方法(当我发表评论时,测试运行没有问题)。
def undo_link
view_context.link_to("Undo archive.", revert_version_path(@answer.versions.scoped.last), :method => :post)
end
该线来自Ryan Bates'Undo with PaperTrail Railscast。它生成的链接似乎在应用程序中完美运行。
以下是rspec测试:
describe 'DELETE #destroy' do
it 'deletes the answer from the database' do
expect{ delete :destroy, :id => @answer1, :question_id => @question1 }.to change(Answer, :count).by(-1)
end
it 'redirects to questionss#show' do
delete :destroy, :id => @answer1, :question_id => @question1
expect(response).to redirect_to @question1
end
end
这是抛出的rspec错误:
1)AnswersController管理员访问DELETE #destroy删除答案 来自数据库
失败/错误:期望{delete:destroy,:id => @ ANSWER1, :question_id => @ question1}。改变(答案,:计数).by(-1)
的ActionController :: UrlGenerationError:
没有路线匹配{:id => nil}缺少必需的键:[:id]
#。/ app/controllers/answers_controller.rb:87:在`undo_link'
#。/ app/controllers/answers_controller.rb:69:在`块(2级)中 破坏'
#。/ app/controllers/answers_controller.rb:68:在`destroy'
#。/ spec / controllers/answers_controller_spec.rb:179:在块(5) 在'
中#。/ spec / controllers / sources_controller_spec.rb:179:在块中(4) 在'
中2)AnswersController管理员访问DELETE #destroy重定向到 questionss#节目
失败/错误:删除:destroy,:id => @ answer1,:question_id => @问题1 ActionController的:: UrlGenerationError: 没有路由匹配{:id => nil}缺少必需的密钥:[:id]
#。/ app/controllers/answers_controller.rb:87:在`undo_link'
# ./app/controllers/answers_controller.rb:69:in `block (2 levels) in destroy' # ./app/controllers/answers_controller.rb:68:in `destroy' # ./spec/controllers/answers_controller_spec.rb:182:in `block (4 levels) in <top (required)>'
任何人都可以解释为什么会发生这种情况以及我能做些什么呢?
编辑 - 2013年12月9日
我在使用better_errors gem的方法中引发了一个错误,以尝试重现nil,正如Mikhail在评论中所建议的那样。我从来没有在应用程序本身中获得nil - 它似乎只出现在rspec测试中。以下是我得到的结果:
>> @answer.versions.scoped.last
=> #<PaperTrail::Version id: 64, item_type: "Answer", item_id: 8, event: "destroy", whodunnit: "6", object: "---\nid: 8\nbody: <p>Shoreditch enim retro, disrupt s...", created_at: "2013-12-09 16:35:08">
>> revert_version_path(@answer.versions.scoped.last)
=> "/versions/64/revert"
>> view_context.link_to("Undo archive.", revert_version_path(@answer.versions.scoped.last), :method => :post)
=> "<a data-method=\"post\" href=\"/versions/64/revert\" rel=\"nofollow\">Undo archive.</a>"
编辑 - 12/10/13
这是设置@ answer1的rspec代码:
before :each do
@question1 = create(:question, :id => 99)
@answer1 = create(:answer, :question_id => 99)
sign_in_user(:admin)
end
工厂:
FactoryGirl.define do
factory :answer do
body { Faker::Lorem.characters(150) }
user_id { 1 }
question_id { 1 }
end
end