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).
Rspec/Capybara "feature" method undefined when Guard runs specs in watched files, works when run manually
-
02-07-2022 - |
Question
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 method
feature' 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'
Solution
OTHER TIPS
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.
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).