Uninitialized Constant Capybara (Nameerror) in Rails App

c:/mowes/www/rails_projects/sample_app/spec/spec_helper.rb:4:in `block in ': uninitialized constant Capybara (NameError)

spec/spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'

RSpec.configure do |config|
  config.include Capybara::DSL
end

I have gem 'capybara', '2.1.0' in my Gemfile so I don't know what's going on.

6 Answers

You have to add config.include Capybara::DSL to rails_helper.rb, and not into spec_helper.rb. It worked for me perfectly!

1

I think this could work. Try adding these lines in spec_helper.rb

RSpec.configure do |config|
 config.fixture_path = "#{::Rails.root}/spec/fixtures"
 config.use_transactional_fixtures = true
 config.infer_base_class_for_anonymous_controllers = false
 config.order = "random" 
 config.include Capybara::DSL
end
1

Did you already run bundle install? Have you added

require 'capybara/rails' 

in rails_helper.rb

If you are using Capybara you might want to follow the instructions here.

0

If you see this error in your specs, despite importing Capybara in spec_helper.rb, adding .rspec with the line --require spec_helper to the root ought to fix the issue:

I'm added config.include Capybara::DSL to rails_helper.rb, and not into spec_helper.rb. and it really helped.

I also countered to this question. I was learning MOOC, and test file was copyed from another place. Because .repec was hide file, so when I copied, this file was not copied. I use Command + shift +. to show the file and copy it to project root directory, and the issue was solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest