【问题标题】:Rspec/Capybara "feature" method undefined when Guard runs specs in watched files, works when run manually当Guard在监视文件中运行规范时,Rspec / Capybara“功能”方法未定义,手动运行时有效
【发布时间】:2013-11-11 23:20:30
【问题描述】:

我在使用 Guard 运行我的规范时遇到了一个奇怪的问题。

我正在运行使用 Capybara“功能”/“场景”语法的功能规范。我也在使用 Spring。

如果我在控制台中运行rspec specspring rspec 或在Guard shell 中运行rspec,一切正常。但是,当监视的规范由 Guard 自动运行时,我收到以下错误:

/spec/features/navigation_spec.rb:1:in <top (required)>': undefined methodfeature' for main:Object (NoMethodError)

为什么它不只在这个特定的上下文中使用 Capybara 语法?

以下是相关代码:

保护文件

guard :rspec, :spring => true do
    watch(%r{^spec/.+_spec\.rb$})
end

spec/features/navigation_spec.rb

feature "navigation" do
    context "When on the home page" do
        before { visit "/" }

        scenario "I should see the navigation header" do
            expect(page).to have_selector("div.navigation")
        end
    end
end

spec/spec_helper.rb

require 'capybara/rspec'

【问题讨论】:

  • 当您尝试从其他上下文中仅运行这一规范时会发生什么?我想知道是不是没有require 'spec_helper.rb
  • 你在做某事。如果我只是从命令行运行一个规范,我会得到同样的错误。那么问题是什么?我还不是很清楚。
  • 没关系。知道了。忘记在功能规范中包含require 'spec_helper'。谢谢!

标签: ruby-on-rails spring rspec capybara guard


【解决方案1】:

在 Rails 4 中,确保您在规范文件顶部包含 'rails_helper' 而不是 'spec_helper'

require 'rails_helper'

feature "Some Feature", :type => :feature do
  ..
end

还要确保 config.disable_monkey_patching! 被注释掉或删除。否则在运行功能规范时会遇到问题。

require 'capybara/rspec'    

RSpec.configure do |config|
  ..
  # config.disable_monkey_patching!
  ..
end

如果您在项目目录中创建了一个 .rspec 文件,请确保也将那里的 spec_helper 更改为 rails_helper

【讨论】:

  • 通过将capybara/rspec 添加到spec/support/ 中的文件中可能更简洁。
【解决方案2】:

对于将来可能遇到类似问题的任何人,我忘记在功能规范中包含 require 'spec_helper'(就像一个白痴一样)。

【讨论】:

  • 在我的情况下,我忘记了spec_helper中的require 'capybara/rspec'...(您错过的线路帮助提醒我检查);)
  • 您现在可以将--require spec_helper 添加到您的.rspec 文件中,您将不再需要在单个规范文件中执行require
【解决方案3】:

你是如何调用守卫的?听起来你可能需要bundle exec guard 才能开始。它也可能在错误的环境下运行(不太可能,但值得一看)。

【讨论】:

    猜你喜欢
    • 2013-07-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多