我是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