我是 factory_girl 的新手,并试图弄清楚如何为以下模型有效地生成工厂:
class Company < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tags < ActiveRecord::Base
has_and_belongs_to_many :companies
validates :type , :inclusion => { :in => %w(market location) }
end
我已经查看了 StackOverflow 上的先前答案(包括这个答案),但是其中大多数已经过时和/或对问题没有正确的答案。有没有人可以帮助用 Factorygirl 为这两个对象定义工厂?
更新
到目前为止,这是我想出的
FactoryGirl.define do
factory :tag do
id 448
trait :market do
type "market"
end
trait :location do
type "location"
end
name "software"
end
factory :company do
id 1234
name "Apple Inc."
factory :company_with_tags do
#setting the default # of tags for companies
ignore do
tag_count 2
end
after(:create) do |company , evaluator|
FactoryGirl.create_list(:tag , evaluator.tag_count , company: company)
end
end
end
end