如何使用Capybara在Rspec功能规范中使用any_instance来存根?

时间:2014-03-10 01:55:12

标签: 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

然而,它始终失败,因为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)>'

1 个答案:

答案 0 :(得分:0)

当我升级到rspec-rails 3.0.0.beta时,此存根功能开始工作。