Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninitialized constant Capybara

I'm having a problem running a test in rails with Capybara. Whenever I run it, it tells me I have an 'uninitialized constant Capybara (NameError)' in my spec_helper.rb file.

I'm following this tutorial: http://www.railstutorial.org/book/static_pages#code-capybara_dsl

This is my spec_helper.rb

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

and I'm trying to run this test static_pages_spec.rb

require 'spec_helper'
  describe "Static pages" do

it "should have the content 'Sample App'" do
  visit '/static_pages/home'
  expect(page).to have_content('Sample App')
end 

If there is any other more information needed just let me know

--------UPDATE----------------

I figured out the problem. The version of Rspec I have creates a rails_helper.rb file in the spec folder. I had to do:

require 'capybara/rspec'

in that file and config.include Capybara::DSL in the configurations.

-----New Problem------------ But now I get another problem it is saying that 'visit' is undefined.

 undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::AboutPage:0x000001033f5d50>

I tried require 'capybara', and require 'capybara/dsl' but they all still give me errors and then some.

like image 745
David Q Avatar asked Nov 01 '22 22:11

David Q


2 Answers

I sort of found out the problem, I'm getting initialized constant capybara because in newer versions of rspec they have a new folder "rails_helper" when you first create a spec folder it creates both rails_helper and spec_helper. The first mock speck test in the spec folder has this at the top

require 'rails_helper'

I was following a tutorial that told me to require 'spec_helper' but that is not true for newer versions of rspec.

So I added

config.include(Capybara::DSL)

in the rails helper folder and everything worked, except I get an error with the css but I believe this is because the tests run headless so they do not work. The spec tests run fine after all the errors are displayed though. If you have any more questions just let me know.

like image 199
David Q Avatar answered Nov 10 '22 06:11

David Q


It's easy to do everything required and then forget to load the Capybara gem via Gemfile. I've done this so this message looked familiar to me.

Perhaps demonstration of this case is helpful to folks; saving some valuable time. :)

In a previously working environment, I hide Capybara by commenting out the capybara line in Gemfile. The result is a similar error message, 'uninitialized constant Capybara (NameError)', which is thrown by rails_helper:

$ rspec spec
Running via Spring preloader in process 17470
/Users/Art/RailsProjects/MyRailsProj/spec/rails_helper.rb:67:in `block in <top (required)>': uninitialized constant Capybara (NameError)

Incidentally, the O.P. may have been using a version of Capybara that preceded the release where two files exist: rails_helper and spec_helper.

In my setup,

config.include(Capybara::DSL)

is in rails_helper.rb.

BTW, if you see examples of the Capybara include in spec_helper, then those pages may be describing the previous file organization.

like image 35
ziff Avatar answered Nov 10 '22 06:11

ziff