【问题标题】:How to stub using any_instance in Rspec feature spec with Capybara?如何使用 Capybara 的 Rspec 功能规范中的 any_instance 存根?
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2017-08-25
    • 2016-11-11
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多