ruby on rails - Testing a simple has many association through a join table -
to wrap exercise i'm working on, i'm trying test association through join table. i'd test association artists have many collections through art pieces , collections have many artists through art pieces. below code:
context 'associations' let(:artist){ artist.create(first_name: "daniel", last_name: "rubio", email: 'drubs@email.com', 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: 'cdent@email.com', no_of_purchases: 10, amount_spent: 100000) } let(:collection) { collection.create(name: 'romanticism') } 'has many collections through art pieces' expect(artist).to respond_to(:collections) art_piece.save! expect(artist.collections).to eql(collection) end end
now i'm positive associations setup correctly on 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 , makes sense me. 2 things think happening either a. have syntax error somewhere or b. variables not being chained properly. insight?
below error message 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">]
artist.collection
returns array-like relation
, while test return single activerecord
object. should have:
expect(artist.collections).to eql([collection])
in test.
Comments
Post a Comment