Rspec/Capybara "feature" method undefined when Guard runs specs in watched files, works when run manually
Asked Answered
M

3

5

I am getting a strange issue when using Guard to run my specs.

I am running a feature spec that uses the Capybara "feature" / "scenario" syntax. I am also using Spring.

Everything works fine if I run rspec spec or spring rspec in the console or rspec in the Guard shell. But, when the watched specs get run automatically by Guard, I get the following error:

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

Why is it not picking up the Capybara syntax only in this specific context?

Here is the relevant code:

GuardFile

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'
Melbourne answered 31/10, 2013 at 19:39 Comment(3)
What happens when you try to run just this one spec from those other contexts? I'm wondering if it's the absence of require 'spec_helper.rb.Quiteri
You are on to something. If I just run the one spec from the command line I get the same error. So what is the issue? It's still not completely clear to me.Melbourne
Nevermind. Got it. Forgot to include require 'spec_helper' in the feature spec. Thanks!Melbourne
M
23

For anyone who may run into a similar issue in the future, I forgot to include require 'spec_helper' in the feature spec (like an idiot).

Melbourne answered 1/11, 2013 at 7:6 Comment(2)
and in my case, I had forgotten require 'capybara/rspec' in the spec_helper... (which your missed line helped remind me to check) ;)Erastus
you can now add --require spec_helper to your .rspec file and you will no longer need to do the require in individual spec files.Fijian
M
12

In Rails 4, make sure that you have included 'rails_helper' instead of 'spec_helper' on top of your specfile:

require 'rails_helper'

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

And also make sure that config.disable_monkey_patching! is commented out or removed. Otherwise you will encounter problems when running your feature specs.

require 'capybara/rspec'    

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

If you have created a .rspec file inside your project dir, also make sure to to change spec_helper to rails_helper there as well.

Midtown answered 23/9, 2014 at 13:7 Comment(1)
It's probably cleaner to require capybara/rspec by adding it to a file in a spec/support/.Ongun
A
0

How are you invoking guard? It sounds like you might need to do bundle exec guard to kick things off. It could also be running under the wrong environment (unlikely, but worth a look).

Angularity answered 31/10, 2013 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.