Shoulda-matchers: have_and_belongs_to_many and inherited models?

Created on 10 Mar 2016  路  2Comments  路  Source: thoughtbot/shoulda-matchers

Hi!

First of all, thank you for providing this great gem! :)

It seems I have an issue when I try to use a "have_and_belongs_to_many" matcher in an inherited model.

My models:

class User < ActiveRecord::Base
end

class Partner < ActiveRecord::Base
    has_and_belongs_to_many :customers
end

class Customer < User
    has_and_belongs_to_many :partners
end

My rspec tests:

RSpec.describe Customer, type: :model do
  it { should have_and_belongs_to_many(:partners) }
end

The it { should have_and_belongs_to_many(:partners) } raise me that failure:
expected #<Customer:0x00000006278818> to respond tohas_and_belongs_to_many?`

I have the same failure when trying to use it the partner model:
expected #<Partner:0x00000008e9b8c8> to respond tohas_and_belongs_to_many?`

RSpec.describe Partner, :type => :model do
  it { should have_and_belongs_to_many :customers }
end

I tried with a has_one and has_many association, the matchers seems to work fine.

Most helpful comment

@Arpsara - Try dropping the "s". It looks like you have have_and_belongs_to_many, the method you want is have_and_belong_to_many

http://matchers.shoulda.io/docs/v3.1.1/Shoulda/Matchers/ActiveRecord.html#have_and_belong_to_many-instance_method

All 2 comments

@Arpsara - Try dropping the "s". It looks like you have have_and_belongs_to_many, the method you want is have_and_belong_to_many

http://matchers.shoulda.io/docs/v3.1.1/Shoulda/Matchers/ActiveRecord.html#have_and_belong_to_many-instance_method

Oh, thank you Adam, that was the problem! :Thanks a lot for your help!

Was this page helpful?
0 / 5 - 0 ratings