Ruby on rails 如何使用Rspec功能规范中的任何_实例与Capybara进行存根?
我在功能规范中有以下内容:Ruby on rails 如何使用Rspec功能规范中的任何_实例与Capybara进行存根?,ruby-on-rails,ruby,rspec,rspec2,Ruby On Rails,Ruby,Rspec,Rspec2,我在功能规范中有以下内容: it "shows the page" do Project.any_instance.stub(:price_all) login_user gso = create(:gs_option) gso.gs_collector.collector.project.update(user_id: @user.id) visit edit_gs_option_path(gso) end 然而,它总是失败的,因为项目中的价格方法
it "shows the page" do
Project.any_instance.stub(:price_all)
login_user
gso = create(:gs_option)
gso.gs_collector.collector.project.update(user_id: @user.id)
visit edit_gs_option_path(gso)
end
然而,它总是失败的,因为项目中的价格方法并没有受到限制。故障跟踪包含以下内容:
# ./app/models/project.rb:430:in `price_all'
如何在Project类上存根price\u all方法
我尝试了stub(:price\u all).使用(anything())
和stub(:price\u all).使用(any\u args())
,但它不会更改失败消息
以下是全部失败:
1) GS Options page shows the page
Failure/Error: visit edit_gs_option_path(gso)
NoMethodError:
undefined method `id' for nil:NilClass
# ./app/models/collector.rb:435:in `price_item'
# ./app/models/gs_collector.rb:279:in `price_out'
# ./app/models/collector.rb:260:in `price_out_all'
# ./app/models/project.rb:430:in `price_all'
# ./app/controllers/application_controller.rb:187:in `get_totals'
# ./app/controllers/gs_options_controller.rb:6:in `edit'
# ./spec/features/gs_options_spec.rb:10:in `block (2 levels) in <top (required)>'
1)GS选项页面显示该页面
失败/错误:访问编辑路径(gso)
命名错误:
nil:NilClass的未定义方法“id”
#./app/models/collector.rb:435:in'price\u item'
#./app/models/gs_collector.rb:279:in'price_out'
#./app/models/collector.rb:260:in'price\u out\u all'
#/app/models/project.rb:430:in'price_all'
#./app/controllers/application\u controller.rb:187:in'get\u totals'
#./app/controllers/gs\u options\u controller.rb:6:在“编辑”中
#./spec/features/gs\u options\u spec.rb:10:in'block(2层)in'
当我升级到rspec rails 3.0.0.beta版时,这个存根功能开始工作。我们能看到完整的错误吗?哪一行是/spec/features/gs\u options\u spec.rb:10
?它是访问编辑选项路径(gs)
行。price\u all
可能是一个类方法吗?如果是这样的话,你就不想在任何项目实例上使用存根了。从这里的一点代码很难判断,但因为这看起来像是一个功能规范,如果你使用的是capybara,并且使用的不是RackTest,那么就有两个独立的进程,你的存根在另一个进程中不会生效。更进一步,如果这是一个使用Capybara的特性/集成/验收测试,您不应该留下任何痕迹。