thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.51k stars 912 forks source link

Rails 3.2 to respond to `has_many?` #890

Closed damienh closed 8 years ago

damienh commented 8 years ago

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?

damienh commented 8 years ago

also I am using mongoid 2.5.2

mcmire commented 8 years ago

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.

jipiboily commented 8 years ago

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

MarkMekhaiel commented 7 years ago

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

violentr commented 7 years ago

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)
mcmire commented 7 years ago

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

violentr commented 7 years ago

@mcmire no i don't have it

mcmire commented 7 years ago

@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

violentr commented 7 years ago

@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

mcmire commented 7 years ago

@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.

ivancortesromero commented 4 years ago

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

mcmire commented 4 years ago

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.