Shoulda-matchers: Problems when running specs with Zeus

Created on 21 Nov 2013  路  29Comments  路  Source: thoughtbot/shoulda-matchers

I'm using [zeus](https://github.com/burke/zeus) to pre-load the code, for when I'm running specs. It appears that zeus isn't properly requiring shoulda-matchers library for some reason.

I see errors like this:

  1) User validations and associations
     Failure/Error: it { should belong_to(:account) }
     NoMethodError:
       undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_3:0x00000104b8d850>
     # ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

  2) User validations and associations
     Failure/Error: it { should accept_nested_attributes_for(:account) }
     NoMethodError:
       undefined method `accept_nested_attributes_for' for #<RSpec::Core::ExampleGroup::Nested_3:0x00000104b94f88>
     # ./spec/models/user_spec.rb:9:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

  3) User validations and associations
     Failure/Error: it { validate_presence_of(:account) }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_3:0x00000104b9c800>
     # ./spec/models/user_spec.rb:10:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'
Need to Investigate

Most helpful comment

That's because shoulda-matchers no longer installs itself into your test framework automatically. You'll need to add this to your rails_helper:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

All 29 comments

This is a bit of a known issue, see #333.

  1. Are the tests running twice, or once?
  2. How are you requiring RSpec in your spec_helper?

Aha, removing require 'rspec/autorun' fixed the issue.

Thanks @mcmire.

this broke when I upgraded from shoulda_matchers 2.5 to 2.6.0 / 2.6.1

Same problem for me as well when I upgrade 1.5.4 to 2.6.0 / 2.6.1.

This is also broken for me - No autorun in my spec helper, and the gem is pulled in after rails-rspec.

same here

I'll reopen this and take another look when I get a chance.

I had the same problem with Zeus, without require 'rspec/autorun' anywhere. My first attempt at a fix was to add this to my spec_helper.rb, as described in the README here:

require 'rspec/rails'
require 'shoulda/matchers'

This didn't fix the problem and I still got undefined method errors.

What did fix the problem was following the README to the point and changing

group :test do
   gem 'shoulda-matchers'
end

in my Gemfile to this too:

group :test do
   gem 'shoulda-matchers', require: false
end

So, in the end following the fix described in the README step by step solved the issue for me.

picard-facepalm2
wfm.
at least now this issue is more easily discoverable

probably an unrelated issue, but following these directions has broken #should_not/#to_not. Rolling back again.

@michaelglass Please file a new issue for this and let me know what error you're getting. Thanks.

I'm going ahead and closing this issue due to inactivity. I'm fairly confident we've nailed this as I haven't seen any more reports from people around this, but I would suggest upgrading to 2.6.2 and seeing if that solves the problem.

same issue on 3.0.0 alpha, rolling back to 2.8.0 solves issue

That's because shoulda-matchers no longer installs itself into your test framework automatically. You'll need to add this to your rails_helper:

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

Still getting this when running with zeus:

NoMethodError: undefined method `belong_to' for #RSpec::ExampleGroups::Business::AssociationSpecs:0x007fba5722d580

I added require: false as suggested, it didn't help

@SergeyBukhman Are you experiencing this on 2.8.0 or 3.0.0.alpha?

2.8.0

(using rails 4.2.1 and shoulda-matchers 2.8.0)

To get it to work, I added a support file with the following:

RSpec.configure do |config|
  config.include Shoulda::Matchers
end

Works now. Added require 'shoulda/matchers' to rails_helper. Not even sure how this worked before without this. Pretty confused about the whole thing, but it works with zeus.

Zeus does the same thing that Spring does: it preloads your gems (as well as your Rails env). You need to prevent shoulda-matchers from preloading b/c otherwise it gets preloaded before RSpec does, so it doesn't automatically mix itself into your example groups. That's why there's a two-step process in the README. You shouldn't have to include anything extra in your rails_helper.rb

group :test do
   gem 'shoulda-matchers', require: false
end

The above works for me. Funnily enough it has been working for other developers.

Adding require: false to the Gemfile declaration and require 'shoulda/matchers to rails_helper.rb worked for me. I'm on shoulda-matchers 2.8.0, Rails 4.2.1, Ruby 2.1.2.

version 2.8.0 solved my problem.

version 2.8.0 solved my problem.

It is working for me

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

Make sure you have type: :model as argument of the describe call in the model spec files.

:+1: Add require: false to gem 'shoulda-matchers', require: false worked for me

Moving the gems around didn't, same with the requires. Thanks!

@CUnknown suggestion of type: :model worked for me in an engine-gem.

I'm on Rails 4.1.6 and shoulda-matchers 3.1.1 and I had to do the new configuration as described in the official documentation and the describe Category, type: :model do bit mentioned above.

Was this page helpful?
0 / 5 - 0 ratings