[英]FactoryGirl with has_many :through associations with capybara
I have the following models: 我有以下型号:
class Productmainclass < ActiveRecord::Base
attr_accessible :name, :id, :maintext
has_many :producttaggings, :dependent => :destroy
has_many :products, :through => :producttaggings
has_many :productsubclasses
end
class Productsubclass < ActiveRecord::Base
attr_accessible :name, :id, :maintext
has_many :producttaggings, :dependent => :destroy
has_many :products, :through => :producttaggings
belongs_to :productmainclass
end
class Product < ActiveRecord::Base
attr_accessible :name, :productimage, :size, :description, :price
has_many :producttaggings, :dependent => :destroy
has_many :productsubclasses, :through => :meteoritetaggings
has_many :productmainclasses, :through => :meteoritetaggings
mount_uploader :productimage, ProductimageUploader
end
class Producttagging < ActiveRecord::Base
belongs_to :product
belongs_to :productsubclass
belongs_to :productmainclass
attr_accessible :product_id, :productsubclass_id, :productmainclass_id
end
I now want to create a Product with FactoryGirl and Capybara. 我现在想用FactoryGirl和Capybara创建一个产品。 In the spec I simply have:
在规范中,我简单地拥有:
product = FactoryGirl.create(:product)
In my factories.rb I have: 在我的factory.rb中,我有:
factory :product do
name "Blue glass"
description "Description text of product"
productimage File.new(File.join(::Rails.root.to_s, "spec/factories/", "testimage.jpg"), 'rb')
productsubclass
productmainclass
end
factory :productsubclass do
name "Colored glasses"
productmainclass
end
factory :productmainclass do
name "Glasses"
end
Running the test I get: 运行测试,我得到:
Failure/Error: product = FactoryGirl.create(:product)
NoMethodError:
undefined method `productsubclass=' for #<Product:0xcd42090>
I think the way you have it setup would work if you were dealing with a situation where :product belonged to productsubclass, then the product and the productsubclass would be created with the product.productsubclass_id nicely inserted and all would be fine, but that's clearly not your structure, so we'd have to use another way. 我认为如果您处理:product属于productsubclass的情况,那么设置方式将可以正常工作,然后将product.productsubclass_id插入良好,就可以创建产品和productsubclass,但一切都很好,但这显然不是您的结构,因此我们不得不使用另一种方法。 I think the link that @depa noted is the right way to go, specifically the 'Basic has many associations' section in this document: http://robots.thoughtbot.com/aint-no-calla-back-girl although you have the added complexity of a has_many through.
我认为@depa指出的链接是正确的方法,特别是本文档中的“基本有很多关联”部分: http ://robots.thoughtbot.com/aint-no-calla-back-girl,尽管您有has_many通过增加的复杂性。 But essentially your looking at a situation where you create an object and then after that you trigger another create to make the many's.
但是从本质上讲,您要先创建一个对象,然后再触发另一个创建该对象的创建。 Hope this makes sense :)
希望这是有道理的:)
** Update ** **更新**
Here's another approach which might be a little limited but you could just create the records from the other direction. 这是另一种方法,可能有些局限,但您可以从另一个方向创建记录。 So, if you just want one record in each object/table how about this:
因此,如果您只想要每个对象/表中的一条记录,该怎么做:
FactoryGirl.define do
factory :producttagging do
product
productsubclass
productmainclass
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.