Shoulda-matchers: Rails 3.2 to respond to `has_many?`

Created on 25 Jan 2016  路  12Comments  路  Source: thoughtbot/shoulda-matchers

Hi,

I am using 2.8.0 within a Rails 3.2.21 and Ruby 2.1.3. I am using rspec-rails 3.1.0. I would use 3.0 but I dont have activesupport 4.0.

I am experiencing the following error:

Failure/Error: it { should have_many(:messages) }
       expected #<User:0x007f671e76f3b8> to respond to `has_many?`

when running a spec with this code:

require 'rails_helper'

RSpec.describe User do
    context "assocations" do
        it { should have_many(:messages) }
    end
 ....

within my rails_helper.rb I have:

require 'shoulda/matchers'

within my user.rb I have the following:

has_many :messages

I have it { should validate_presence_of(:first_name) } working so the gem is loading, but I cannot work out why the association tests are failing.

Can anyone give me any insight?

Most helpful comment

Btw, I'm using ActiveRecord but today while upgrading Rails and every other gem in one app I'm maintaining, it broke for me too (no Mongoig)...and this worked for me:

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end

I have no idea which gem upgrade broke it for me, but thought I would still share that this workaround worked like a charm. Cheers

All 12 comments

also I am using mongoid 2.5.2

shoulda-matchers doesn't explicitly support Mongoid. It was designed for ActiveRecord, and the gem will make ActiveRecord-specific matchers (such as have_many, belong_to, validate_uniqueness_of, etc.) available for use in example groups automatically only if ActiveRecord is loaded.

You can _try_ explicitly including the modules that provide the matchers into example groups tagged with :model like so:

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end

...but there's no guarantee that these matchers will work.

Sorry about that.

Btw, I'm using ActiveRecord but today while upgrading Rails and every other gem in one app I'm maintaining, it broke for me too (no Mongoig)...and this worked for me:

RSpec.configure do |config|
  config.include(Shoulda::Matchers::ActiveRecord, type: :model)
end

I have no idea which gem upgrade broke it for me, but thought I would still share that this workaround worked like a charm. Cheers

Had same issue - @jipiboily's solution fixed it for me.

Interesting thread, having same issue @jipiboily's solution helped me.
I tried both ways with 'should' and with 'is_expected' was not working unless
put this line into rspec/spec_helper file.

 config.include(Shoulda::Matchers::ActiveRecord, type: :model)

@violentr Do you have a Shoulda::Matchers.configure block in your rails_helper?

@mcmire no i don't have it

@violentr Cool -- that's the issue then. Starting with 3.0 you must manually configure the gem in order to make those matchers available to your example groups. See the README for more: https://github.com/thoughtbot/shoulda-matchers#rspec

@mcmire i have shoulda-matchers version 3.1.1 and i have no Shoulda::Matchers.configure set in my rails_helper , instead config.include(Shoulda::Matchers::ActiveRecord, type: :model) line set to my spec/spec_helper

@violentr Right. What I'm saying is that you don't have to manually include Shoulda::Matchers::ActiveRecord into model example groups. The gem will already do this for you as long as you have the Shoulda::Matchers.configure block in your rails_helper as indicated in the README. You should only need to do a manual include as a very last resort.

Hi @mcmire, I have Shoulda::Matchers.configure block in my rspec_helper.rb and the error is raised.

gem version: 4.4.0
rails version: 6.0.3

Hi @ivancortesromero, 4.4.0 had a regression in it where matchers did not get included into example groups. If you switch to 4.4.1 then the issue should be fixed.

Was this page helpful?
0 / 5 - 0 ratings