Dan Rubio
Dan Rubio

Reputation: 4897

Testing a simple has many association through a join table

To wrap up a exercise I'm working on, I'm trying to test an association through a join table. What I'd like to do is to test the association that Artists have many collections through Art Pieces and Collections have many Artists through Art Pieces. Below is my code:

context 'associations' do

let(:artist){ Artist.create(first_name: "Daniel", last_name: "Rubio", 
              email: '[email protected]', birthplace: 'Mexico', 
              style: 'Contemporary') }

let(:art_piece) { ArtPiece.new(date_of_creation: DateTime.parse('2012-3-13'),    
                  placement_date_of_sale: DateTime.parse('2014-8-13'), 
                  cost: 250, medium: 'sculpture', availability: true, artist_id: 
                  artist.id, customer_id: customer.id, 
                  collection_id: collection.id)}

let(:customer) { Customer.create(first_name: 'Carmen', last_name: 'Dent', email:  
                '[email protected]', no_of_purchases: 10, amount_spent: 100000) }

let(:collection) { Collection.create(name: 'Romanticism') }

it 'has many collections through art pieces' do
  expect(artist).to respond_to(:collections)
  art_piece.save!
  expect(artist.collections).to eql(collection)
end

end 

Now I'm positive my associations are setup correctly on my validations:

class Collection < ActiveRecord::Base
  validates_presence_of :name

  has_many :art_pieces
  has_many :artists, through: :art_pieces
end

class Artist < ActiveRecord::Base
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates :email, presence: true, uniqueness: true
  validates_presence_of :style
  validates_presence_of :birthplace

  has_many :art_pieces
  has_many :collections, through: :art_pieces
end

I followed activerecord's guidelines and it makes sense to me. The only two things that I think is happening is either A. I have a syntax error somewhere or B. my variables are not being chained properly. Any insight?

Below is the error message I get:

1) Artist associations has many collections through art pieces
 Failure/Error: expect(artist.collections).to eql(collection)

   expected: #<Collection id: 20, name: "Romanticism", created_at: "2014-04-06 16:57:42", updated_at: "2014-04-06 16:57:42">
        got: #<ActiveRecord::Associations::CollectionProxy [#<Collection id: 20, name: "Romanticism", created_at: "2014-04-06 16:57:42", updated_at: "2014-04-06 16:57:42">]>

   (compared using eql?)

   Diff:
   @@ -1,2 +1,2 @@
   -#<Collection id: 20, name: "Romanticism", created_at: "2014-04-06 16:57:42", updated_at: "2014-04-06 16:57:42">
   +[#<Collection id: 20, name: "Romanticism", created_at: "2014-04-06 16:57:42", updated_at: "2014-04-06 16:57:42">]

Upvotes: 0

Views: 2251

Answers (2)

Tinmar
Tinmar

Reputation: 31

Try changing

    expect(artist.collections).to eql(collection)

to

    expect(artist.collections).to eql([collection])

Currently your rspec is expecting a single collection object but what your query returns is an array like object with a single element in it. Changing the expectation to a list of your collection should do the trick.

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51151

artist.collection returns array-like Relation, while you test it to return single ActiveRecord object. You should have:

expect(artist.collections).to eql([collection])

in your test.

Upvotes: 1

Related Questions