当Guard在监视文件中运行规范时,Rspec/Capybara"feature"方法未定义,在手动运行时有效

dpd*_*son 5 spring rspec ruby-on-rails guard capybara

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

我正在运行使用Capybara"feature"/"scenario"语法的功能规范.我也在使用Spring.

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

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

为什么它只是在这个特定的上下文中才取出Capybara语法?

这是相关代码:

GuardFile

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

规格/功能/ 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
Run Code Online (Sandbox Code Playgroud)

投机/ spec_helper.rb

require 'capybara/rspec'
Run Code Online (Sandbox Code Playgroud)

dpd*_*son 23

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

  • 在我的情况下,我忘记了`spec_helper`中的'require'capybara/rspec'`(你错过的行帮助我提醒我检查);) (2认同)

kar*_*gen 12

在Rails 4中,确保您已包含'rails_helper'而不是'spec_helper'在specfile之上:

require 'rails_helper'

feature "Some Feature", :type => :feature do
  ..
end
Run Code Online (Sandbox Code Playgroud)

并确保config.disable_monkey_patching!已注释掉或删除.否则,在运行功能规范时会遇到问题.

require 'capybara/rspec'    

RSpec.configure do |config|
  ..
  # config.disable_monkey_patching!
  ..
end
Run Code Online (Sandbox Code Playgroud)

如果您已经创建了项目目录内的文件.rspec,也一定要改spec_helperrails_helper那里.