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!
Related
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
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
I am using rails console in the development environment and I want to use factories. How can I get access to them?
I have tried require "FactoryGirl" which returns
1.9.3p393 :301 > require "FactoryGirl"
LoadError: cannot load such file -- FactoryGirl
I do this the following way:
Start the rails console in test environment in sandbox mode.
rails console -e test --sandbox
You need this for two reasons:
Any changes you do are rolled back.
If you already have some seed data it might happen that the factories will start the serialization of attributes from 1, but these records might already exist.
Then in the console:
Require FactoryBot (was called FactoryGirl):
require 'factory_bot'
Load the factory definitions:
FactoryBot.find_definitions
Include the FactoryBot methods to avoid prefixing all calls to FB with FactoryBot (create instead of FactoryBot.create):
include FactoryBot::Syntax::Methods
P.S. For fabrication gem you can load the definitions in the rails console with:
Fabrication.manager.load_definitions
Also require 'faker' if you use it.
To solve this problem ensure that the factory bot gem is specifed in your Gemfile similar to this
group :development, :test do
gem 'factory_bot_rails'
end
Then bundle install.
This should make FactoryBot class available in the development console.
Hope this helps.
You need to require 'factory_bot_rails', which is the actual gem that's being used by Rails. That gem will include the Factory Bot library, making FactoryBot available.
You can either do this, or update your Gemfile to require it at startup as in muttonlamb's answer.
If you want to have it available each time you start the console, you can add this piece of code to the top of your config/environments/development.rb:
require 'factory_bot_rails'
require 'faker' # if you're also using faker gem
require 'rails/console/helpers'
Rails::ConsoleMethods.prepend(FactoryBot::Syntax::Methods)
Now you can use the built-in helpers right after starting the console, for example:
company = create(:company)
I am learning Ruby and have to do some homework, but have problem with executing code. I searched this forum and Google and found that this could be problem with different Ruby versions but I don't know how to fix this. I've tried to execute:
cucumber features/filter_movie_list.feature
and got this:
And I should not see 'G' #
features/step_definitions/movie_steps.rb:32
no such file to load -- rspec/matchers/built_in/has (LoadError)
./features/step_definitions/movie_steps.rb:34:in /^(?:|I )should not see '([^']*)'$/'
features/filter_movie_list.feature:37:inAnd I should not see 'G''
Can someone help me to solve this problem or give me link to solution?
EDIT:
movie_steps.rb relevant for this taks is:
Then /^(?:|I )should not see '([^']*)'$/ do |text|
if page.respond_to? :should
page.find('#movies').should have_no_content(text)
else
assert page.find('#movies').has_no_content?(text)
end
end
The error message says that Cucumber can't find RSpec.
To solve it, edit your Gemfile and add these and that should solve it.
group :test do
gem 'rspec' # Behavior Driven Development (BDD) for Ruby
gem 'rspec-core' # RSpec runner and example groups.
gem 'rspec-expectations' # RSpec matchers for should and should_not.
gem 'rspec-mocks' # RSpec test double framework with stubbing and mocking.
gem 'rspec-rails' # RSpec version 2.x for Rails version 3.x.
end
If that does solve it, you can probably remove all those lines except the first one (the 'rspec' one) because that line should take care of the others.
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)