Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shoulda-matchers not working with spring #486

Closed
ghost opened this issue Apr 15, 2014 · 3 comments
Closed

shoulda-matchers not working with spring #486

ghost opened this issue Apr 15, 2014 · 3 comments
Milestone

Comments

@ghost
Copy link

ghost commented Apr 15, 2014

Not sure if this is a spring issue or a shoulda-matchers issue.

On Rails 4.1, spring 1.1.2 and shoulda-matchers 2.6.0 none of the matchers from shoulda-matchers seem to work:

     Failure/Error: it { should validate_presence_of(:first_name) }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcafe53a018>

Here is an example app with spring setup https://github.com/Soliah/foo

Running bundle exec rspec spec works fine, but using the spring binstub for rspec bin/rspec causes the undefined method error.

@avdgaag
Copy link

avdgaag commented Apr 15, 2014

I have this same issue. There is a workaround by requiring shoulda-matchers a little later. So in your Gemfile do:

gem 'shoulda-matchers', require: false

...and then in spec/spec_helper require it manually:

require 'shoulda-matchers'

Since Rails 4.1 ships with Spring by default, it would be nice to note this in the README.

@mcmire
Copy link
Collaborator

mcmire commented Apr 15, 2014

Thanks for the report.

Yeah, shoulda-matchers needs to be loaded after RSpec is loaded, so that it can detect it and inject itself into the appropriate example groups.

This should automatically happen when Bundler requires your gems, because shoulda-matchers is loaded after rspec-rails. However, requiring rspec-rails, it turns out, doesn't actually load all of RSpec until you require 'rspec/rails' in your spec_helper. By that point, shoulda-matchers is already loaded so it doesn't auto-insert itself.

Why has this always worked before, then? Because the rspec executable loads all of RSpec, so it's there before your spec_helper is even loaded.

Why doesn't this now work? Because Spring flips it around: spec_helper (and your Gemfile) are loaded before the rspec executable is run.

/cf #382

@mcmire
Copy link
Collaborator

mcmire commented Apr 16, 2014

Closing in favor of #490.

@mcmire mcmire closed this as completed Apr 16, 2014
mcmire added a commit to thoughtbot/shoulda that referenced this issue Jun 20, 2014
The instructions on how to install shoulda-matchers in your project have
changed: for best results, now you must manually require it in your
spec_helper (which has actually become rails_helper in RSpec 3.x).

For more information:
<thoughtbot/shoulda-matchers#486 (comment)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants