【发布时间】:2014-04-13 00:09:44
【问题描述】:
我在功能规范中有以下内容:
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
然而,它总是失败,因为 Project 上的 price_all 方法没有被存根。失败跟踪包含以下内容:
# ./app/models/project.rb:430:in `price_all'
如何对 Project 类的 price_all 方法存根?
我尝试了stub(:price_all).with(anything()) 和stub(:price_all).with(any_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)>'
【问题讨论】:
-
我们能看到完整的错误吗?
-
/spec/features/gs_options_spec.rb:10是哪一行? -
这是
visit edit_gs_option_path(gs)行。 -
price_all是一个类方法吗?如果是这样,您不想在 Project 的任何 instance 上将其存根。 -
很难从这里的一小段代码中分辨出来,但由于这看起来像一个特性规范,如果你使用 capybara 并使用 RackTest 以外的任何东西,那么有两个单独的进程,你的存根将在其他过程中无效。更进一步,如果这是 Capybara 的功能/集成/验收测试,您不应该存根任何东西。
标签: ruby-on-rails ruby rspec rspec2