Uninitialized constant Capybara - ruby-on-rails

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.

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.

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.

Add require 'capybara/rails' to your test helper

Related

Why Rspec cannot find the select2 method?

I've just joined a new company and when I'm using the select2 method in Rspec, I get the following error: NoMethodError:undefined method `select2' for...
We use the 'capybara-select-2' gem.
So I added in spec_helper.rb the following config: config.include CapybaraSelect2 and it's perfectly works. Problem, when tests are pushed on heroku, it raises an error: uninitialized constant CapybaraSelect2.
The weird thing is my colleague doesn't need to add the config line for using this method...
Do you have any idea why he can use it without it and I can't? Thanks for your help.
Your coworker probably did gem install capybara-select-2 at some point installing it globally into his environment, while you're using it via bundler. You should make sure it has been included into the :test group in your gemfile.
That being said I wouldn't recommend using that gem anymore, it hasn't been updated in years.
My CTO finally found a way to make my code working:
I was calling config.include CapybaraSelect2 at the end of spec_helper.rb so he moved it within rails_helper.rb, just as following:
config.include Devise::Test::IntegrationHelpers
config.include ActiveSupport::Testing::TimeHelpers
config.include ::Passwordless::TestHelpers::SystemTestCase
config.include CapybaraSelect2
Everything works fine now!

NameError: unitialized constant ServiceName with default rspec configuration

I created rails new --api whatever with:
gem 'rspec-rails', '~> 3.8'
in my Gemfile. Then, I created:
app/services/whatever_module/whatever_class.rb
and corresponding spec file:
spec/services/whatever_module/whatever_class_spec.rb
Now, when I run:
rspec services
I get this error:
NameError: uninitialized constant WhateverModule
How do I tell rspec to recognize the module by its spec path?
Your spec file should be in
spec/services/distamce/whatever_class_spec.rb.
In you case rspec tries to find the WhateverModule because of /whatever_module/ in your pathing for the spec file. You can try this to change to spec/services/foo_bar/whatever_class_spec.rb and you will get the missing FooBarModule error.
I think I figured out what you missed.
Rspec does not automatically require your app folder and so there are no modules or classes available from the app folder initially.
When you check https://github.com/rspec/rspec-rails#installation #2 then you can see you have to add some boilerplate files for rspec like rails_helper.rb and spec_helper.rb with rails generate rspec:install. These are responsible for all rspec related settings and the require of the app folder.
Also its required to add require 'rails_helper' on top of each spec file.
After you have done all this and you get the error Unable to autoload constant WhateverModule::WhateverClass then your whatever_class.rb has to look like this
module WhateverModule
class WhateverClass
end
end
Or you define the module in a file besides the whatever_module folder.

Guard + Zeus + Rspec-Rails: undefined method 'configure' for Rspec:Module

I'm using the following:
Rails 4.1.1
guard-zeus 2.0.0
rspec-rails 3.0.1
Out of box default rails g rspec:install and guard init
When I run guard and save a spec file, I get the error:
undefined method `configure` for RSpec:Module (NoMethodError)
I can run specs with rspec spec and rake just fine.
In spec_helper, if I require 'rspec/rails before the configure block,
guard works fine, but then rspec spec fails with the error:
uninitialized constant ActiveSupport::Autoload (NameError)
I'm guessing there's a problem with load order now that rails_helper and spec_helper
are separated.
Two questions:
How can I fix this?
Is there a different solution for continuous integration locally that you can recommend that works with latest Rails and Rspec.
You only have to answer one question.
The following fix worked for me:
#spec/spec_helper.rb
require 'rspec/core'
Throwing out a quick answer that may be the problem. Your spec_helper file should have the following order:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
rspec/rails needs to be required after the config/environment require.
The following:
undefined method `configure` for RSpec:Module (NoMethodError)
suggests you are are missing a
require 'rspec'
This normally isn't necessary, but if you put it in your spec/spec_helper.rb that should work.
(If you run RSpec directly, it's included already with RSpec).
The reason it's not included is perhaps:
you are not running guard through bundler
or your Gemfile does not have:
gem 'rspec' # without the require: false
or something may be wrong with your .rspec file (which should be present)
The require 'rspec/rails' should probably go into the spec/rails_helper.rb...
... but a better way would be to update your rspec-rails gem and run:
rails generate rspec:install
and if you're prompted - used 'd' to differences (and ideally use the recommended changes).
You should add following require to top of file spec_helper.rb
require 'rspec/rails'
Take the reference here: Zeus GitHub issue 308

Capybara tests fail with Timeout::Error in the first test when using Google Maps

I am testing with RSpec + Capybara + Selenium (Firefox). No matter what subset of my acceptance tests I run, the first one fails (next tests do work correctly) with "reason" like this:
Failure/Error: visit '/'
Timeout::Error:
Timeout::Error
My application relies on GoogleMaps and BackboneJS heavily. When I run tests, page does not finish to load and "transferring data from maps.googleapis.com" message stays in bottom-left of Firefox window, however page looks like it loaded properly (maps and other content are present). I've already set timeout to 60 secs to exclude any slow network problems. And all the subsequent tests do work very fast (like proper Google scripts were fetched and are already cached). Also when I start server in development environment and access it (localhost:3000) everything works fine.
Firefox 17.0.1. Some of my gems:
capybara (2.0.1)
database_cleaner (0.9.1)
mongoid (3.0.13)
rspec (2.12.0)
rspec-core (2.12.1)
rspec-expectations (2.12.0)
rspec-mocks (2.12.0)
rspec-rails (2.12.0)
selenium-webdriver (2.26.0)
Do you have any idea why this happens and how to prevent it?
EDIT:
spec/spec_helper.rb:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.treat_symbols_as_metadata_keys_with_true_values = true
config.infer_base_class_for_anonymous_controllers = false
#config.order = "random"
end
spec/features/features_spec_helper.rb:
require_relative "../spec_helper"
require 'capybara/rspec'
Capybara.default_driver = :selenium
Capybara.default_wait_time = 60
EDIT2:
It used to work properly before (few weeks ago; this project was halted for that time). Could be introduced by RSpec upgrade from 2.11 to 2.12 (which I did those few weeks ago), but I've just tried to downgrade it and same things do happen. I've reverted whole codebase to the point month ago to exclude possible gem regression. Problem still happens.
EDIT3:
I've just discovered that if I comment out the line responsible for attaching map from Google:
new google.maps.Map($("#map")[0], mapOptions)
then everything works like a charm.
EDIT4:
Source code of example application: https://github.com/skalee/capybara-google-maps-failure
Running all specs will result with Timeout::Error in the first one (at least for me). However, all specs will pass when:
You remove line which initializes Geocoder (it's not used in example app, but I use it in mine).
You remove line which initializes Map.
What surprised me most, when you provide trivial style sheet, like nothing more than that:
#map{ width: 600px ; height: 600px }
Gems are exactly the same gems as I use in my app. There are some 3rd party scripts in /vendor/assets.
You should not depend on external services for your tests. Here is a strategy:
http://robots.thoughtbot.com/post/34761570235/using-capybara-to-test-javascript-that-makes-http
Have you tried changing the GemFile around to have a group :production do ?
And are you loading this whole script in the proper place?
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false">
This example is for bootstrap:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE"
type="text/javascript">
</script>

How to access Clearance Shoulda macros in RSpec

I can't work out how to get access to the shoulda macros (sign_in_as, etc) in my RSpec tests. I've installed and unpacked the shoulda gem into vendor and I've run the generator to install clearance. I suspect I need to use the right require statement in spec_helper.rb but nothing I've been able to find works for me. I've completely run out of ideas.
I got it to work by following the instructions on this blog post: http://blog.smajnr.net/2011/03/clearance-rspec-shoulda.html
# in spec_helper.rb
require 'clearance/shoulda_macros'
RSpec.configure do |config|
# ...
# Include Shoulda macros for Clearance
config.include Clearance::Shoulda::Helpers
end
Add the following to to the top of your spec_helper.rb,
require 'clearance/shoulda_macros'
And then inside the Rspec configure block add the following,
config.extend(Clearance::Shoulda::Helpers)

Resources