Ruby on rails Rails 4升级后,view_上下文在应用程序中运行,但测试失败。为什么?

Ruby on rails Rails 4升级后,view_上下文在应用程序中运行,但测试失败。为什么?,ruby-on-rails,rspec,ruby-on-rails-4,railscasts,paper-trail-gem,Ruby On Rails,Rspec,Ruby On Rails 4,Railscasts,Paper Trail Gem,我刚刚将我的应用程序从Rails 3.2升级到Rails 4 我正在努力完成需要做出的各种改变,但这一点让我感到困惑 我的销毁操作似乎在应用程序中运行,但当我运行rspec测试时,它们失败了 这就是我的控制器中似乎抛出错误的方法(当我注释掉它时,测试运行没有问题) 线路取自Ryan Bates的铁路公司。它生成的链接在应用程序中似乎运行良好 以下是rspec测试: describe 'DELETE #destroy' do it 'deletes the answer from the da

我刚刚将我的应用程序从Rails 3.2升级到Rails 4

我正在努力完成需要做出的各种改变,但这一点让我感到困惑

我的销毁操作似乎在应用程序中运行,但当我运行rspec测试时,它们失败了

这就是我的控制器中似乎抛出错误的方法(当我注释掉它时,测试运行没有问题)

线路取自Ryan Bates的铁路公司。它生成的链接在应用程序中似乎运行良好

以下是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,:id=>@answer1, :question_id=>@question1}。要更改(答案::count)。通过(-1)

ActionController::UrlGenerationError:

没有路由匹配{:id=>nil}缺少必需的键:[:id]

#./app/controllers/answers\u controller.rb:87:in'undo\u 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)>'
#./app/controllers/answers\u controller.rb:69:in`block(2层)in` 摧毁

#/app/controllers/answers\u controller.rb:68:in'destroy'

#/spec/controllers/answers\u controller\u spec.rb:179:in`block(5 级别)在'

#/spec/controllers/answers\u controller\u spec.rb:179:in`block(4 级别)在'

2) AnswersController管理员访问删除#销毁重定向到 问题秀

失败/错误:delete:destroy,:id=>@answer1,:question\u id=> @问题1 ActionController::UrlGenerationError: 没有路由匹配{:id=>nil}缺少必需的键:[:id]

#./app/controllers/answers\u controller.rb:87:in'undo\u 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)>'
工厂:

FactoryGirl.define do
  factory :answer do
    body { Faker::Lorem.characters(150) }
    user_id { 1 }
    question_id { 1 }
  end
end

似乎
@answer.versions.scoped.last
返回规范中的
nil
,也许您应该从这一点开始调查。谢谢,米哈伊尔。我在方法中提出了一个错误,并使用更好的_errors gem对其进行了一系列测试(我将把它们添加到问题中)。据我所知,应用程序本身似乎并没有出现这种零。为什么会发生在测试中?你能分享设置
@answer1
中涉及的规范代码吗?我刚刚添加了规范代码,Peter。谢谢你看!
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