shoulda-matchers: Broken Rspec Integration 3.0.0
Hi, I just installed this gem and following the readme, but. it gives this error while running rspec. uninitialized constant Shoulda (NameError)
This is my rails_helper.rb
Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
# Or, choose the following (which implies all of the above):
with.library :rails
end
end
Any idea?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 23
Commits related to this issue
- fix shoulda matchers configuration Thanks to https://github.com/thoughtbot/shoulda-matchers/issues/803#issuecomment-148856076 — committed to Oladele/noovis_app2 by Oladele 9 years ago
I was having same issues. Solution: I had a group “test” in the Gemfile for which the shoulda-matcher was defined, but was working all the time on development environment. Just changed the gem definition to :development, :test group and bundle install again. Worked like a charm.
The shoulda-matchers configuration should not be added to spec_helper, but to rails_helper.
When you run the
rspec
executable, because you have--require spec_helper
in.rspec
, RSpec will loadspec_helper
first before it tries to run any files. The goal ofspec_helper
is to set up any tests that don’t need Rails to run; therefore, it will not load your Rails environment or automatically require any gems in your Gemfile, including shoulda-matchers. This is why theShoulda
constant isn’t available. To fix this, move that configuration block into rails_helper (which does set up a Rails environment) and everything should be golden.You need add those new contents to rails_helper.rb instead of replacing the original code rails_helper should be like this (it works for me):
I’ve had the same issue, the only way I’ve been able to solve the issue is to use the full line,
RAILS_ENV=test bundle exec rspec
EDIT: Changing the location to
group :development, :test
also works, but this seems odd to me. The test suite feels like it should be in test only. I guess it is just a matter of semantics.@mcmire, I am having this issue. I’m using Ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-darwin16] and Rails 5.1.3 shoulda-matchers are included in my gemfile like this:
(I did have shoulda-matchers line in
group :test do
instead earlier, with the same error.Here are the errors:
Here is my rails_helper:
Ah yes. I think my problem is I added the configuration at
spec_helper.rb
Yes. It is a fresh install.
I’m not sure why you’d be getting that error. I’m assuming you have
shoulda-matchers
in your Gemfile?