Ruby on rails 通过联接表测试一个简单表有许多关联

Ruby on rails 通过联接表测试一个简单表有许多关联,ruby-on-rails,rspec,associations,Ruby On Rails,Rspec,Associations,为了结束我正在进行的一个练习,我正在尝试通过联接表测试关联。我想做的是测试一下这个协会,艺术家通过艺术作品拥有很多收藏品,而收藏品通过艺术作品拥有很多艺术家。下面是我的代码: context 'associations' do let(:artist){ Artist.create(first_name: "Daniel", last_name: "Rubio", email: 'drubs@email.com', birthplace: 'Mexico',

为了结束我正在进行的一个练习,我正在尝试通过联接表测试关联。我想做的是测试一下这个协会,艺术家通过艺术作品拥有很多收藏品,而收藏品通过艺术作品拥有很多艺术家。下面是我的代码:

context 'associations' do

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') }

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 
现在我确信我的关联在我的验证中设置正确:

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
类集合
我遵循了activerecord的指导原则,这对我来说很有意义。我认为发生的两件事要么是A.我在某处有语法错误,要么是B.我的变量没有正确链接。有什么见解吗

下面是我收到的错误消息:

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">]
1)艺术家协会通过艺术作品收藏了许多作品
失败/错误:预期(艺术家收藏)。到eql(收藏)
预期:#
得到:#
(使用eql进行比较?)
差异:
@@ -1,2 +1,2 @@
-#
+[#]

artist.collection
Relation
一样返回数组,同时测试它以返回单个
ActiveRecord
对象。你应该:

expect(artist.collections).to eql([collection])
在你的测试中

试试换衣服

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


当前,您的rspec需要一个集合对象,但您的查询返回的是一个类似数组的对象,其中只有一个元素。将期望值更改为集合列表应该可以做到这一点。

错误消息是什么?我将响应部分更改为:集合,因为这是我调用的方法,但后来我得到了这个。你知道这是什么意思吗?嘿,Marek,那一行代码对我来说仍然不起作用,但我只是随便看看,我用这一行通过了测试。expect(artist.collections).to include(collection)..include(collection)使用起来有意义吗?@DanRubio是的,它有意义。正如我在上面为Marek所评论的那样,这行代码对我不起作用,但expect(artist.collections).to include(collection)使用起来有意义。当我考虑它时,它应该是一个数组是有道理的,但是没有用。谢谢你的帮助。
    expect(artist.collections).to eql([collection])