我知道在定义Factory时如何创建has_many关联:
factory :user do
name "John Doe"
factory :user_with_posts do
ignore do
posts_count 5
end
after(:create) do |user, evaluator|
create_list(:post, evaluator.posts_count, user: user)
end
end
end
但是当我实际创建工厂时,我该怎么做呢,例如:
Factory.create(:user, :posts << ??)
答案 0 :(得分:1)
一种方法是使用这样的块:
FactoryGirl.create(:user) do |user|
FactoryGirl.create_list(:post, 10, user: user)
end