Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara 2.1 Error uninitialized constant Rails (NameError)

I just upgraded my Capybara Gem from version 1 to 2.1.0 (latest). Based on Capybara Readme, I added this following lines to my spec_helper.rb inside Spork.prefork block

require 'capybara/rspec'
require 'capybara/rails'

But, I got an error

/home/user_1/.rvm/gems/ruby-1.9.3-p392/gems/capybara-2.1.0/lib/capybara/rails.rb:6:in `block (2 levels) in <top (required)>': uninitialized constant Rails (NameError)

Did I miss something in order capybara to work properly ?

like image 418
Glend Maatita Avatar asked Aug 20 '13 00:08

Glend Maatita


2 Answers

Make sure you require 'rspec/rails' first:

require 'rspec/rails'

require 'capybara/rspec'
require 'capybara/rails'

If you require 'capybara/rails' first, you'll get that error.

like image 191
bowsersenior Avatar answered Oct 18 '22 18:10

bowsersenior


In your spec_helper.rb file, this error can be caused from adding the require 'capybara/rails' line in the top of the file's code.

Instead, if you look about half-way down that auto-generated spec_helper.rb file, you will see something like this:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

Add the require 'capybara/rails' line after those lines so that it looks like:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails' #######

(you don't need the hashes, those were just to highlight the insertion location)

Hopefully this helps you or someone else out there...

like image 38
cjn Avatar answered Oct 18 '22 20:10

cjn