mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Document attribute groups
This commit is contained in:
parent
7c5ec969c5
commit
86125a4448
1 changed files with 72 additions and 0 deletions
|
@ -259,6 +259,78 @@ Without a block, the value will increment itself, starting at its initial value:
|
|||
sequence(:position)
|
||||
end
|
||||
|
||||
Attribute Groups
|
||||
----------------
|
||||
|
||||
Attribute groups allow you to group attributes together and then apply them
|
||||
to any factory.
|
||||
|
||||
factory :user, :aliases => [:author]
|
||||
|
||||
factory :story do
|
||||
title "My awesome story"
|
||||
author
|
||||
|
||||
attribute_group :published do
|
||||
published true
|
||||
end
|
||||
|
||||
attribute_group :unpublished do
|
||||
published false
|
||||
end
|
||||
|
||||
attribute_group :week_long_publishing do
|
||||
start_at { 1.week.ago }
|
||||
end_at { Time.now }
|
||||
end
|
||||
|
||||
attribute_group :month_long_publishing do
|
||||
start_at { 1.month.ago }
|
||||
end_at { Time.now }
|
||||
end
|
||||
|
||||
factory :week_long_published_story, :attribute_groups => [:published, :week_long_publishing]
|
||||
factory :month_long_published_story, :attribute_groups => [:published, :month_long_publishing]
|
||||
factory :week_long_unpublished_story, :attribute_groups => [:unpublished, :week_long_publishing]
|
||||
factory :month_long_unpublished_story, :attribute_groups => [:unpublished, :month_long_publishing]
|
||||
end
|
||||
|
||||
Attribute groups can be used as attributes:
|
||||
|
||||
factory :week_long_published_story_with_title, :parent => :story do
|
||||
published
|
||||
week_long_publishing
|
||||
title { "Publishing that was started at {start_at}" }
|
||||
end
|
||||
|
||||
Attribute groups that define the same attributes won't raise AttributeDefinitionErrors;
|
||||
the attribute group that defines the attribute latest in order gets precedence.
|
||||
|
||||
factory :user do
|
||||
name "Friendly User"
|
||||
login { name }
|
||||
|
||||
attribute_group :male do
|
||||
name "John Doe"
|
||||
gender "Male"
|
||||
login { "#{name} (M)" }
|
||||
end
|
||||
|
||||
attribute_group :female do
|
||||
name "Jane Doe"
|
||||
gender "Female"
|
||||
login { "#{name} (F)" }
|
||||
end
|
||||
|
||||
attribute_group :admin do
|
||||
admin true
|
||||
login { "admin-#{name}" }
|
||||
end
|
||||
|
||||
factory :male_admin, :attribute_groups => [:male, :admin] # login will be "admin-John Doe"
|
||||
factory :female_admin, :attribute_groups => [:admin, :female] # login will be "Jane Doe (F)"
|
||||
end
|
||||
|
||||
Callbacks
|
||||
---------
|
||||
|
||||
|
|
Loading…
Reference in a new issue