I’m still discovering the joys of using FactoryGirl to create test data but I particularly like this pattern for creating a collection of associated objects.

[code lang=ruby]
FactoryGirl.define do
factory :list do
name “My List”
end

trait :with_items do
ignore do
item_count 3
end

after(:create) do |plan, evaluator|
FactoryGirl.create_list(:item, evaluator.item_count, list: list)
end
end
end
[/code]

Now, I can call

[code lang=ruby]
FactoryGirl.create :list
[/code]

to create an empty list,

[code lang=ruby]
FactoryGirl.create :list, :with_items
[/code]

to create a list with 3 items or I can even specify the number of items using

[code lang=ruby]
FactoryGirl.create :list, :with_items, :item_count 50
[/code]

I’m also fond of naming my traits :with_xxxx as it reads well like in the examples above