mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Adds traits as mixins to 'Getting started' (#1521)
This commit is contained in:
parent
1b81d5dc25
commit
dc65e786a9
1 changed files with 22 additions and 0 deletions
|
@ -1196,6 +1196,28 @@ factory :user do
|
|||
end
|
||||
```
|
||||
|
||||
### As mixins
|
||||
|
||||
Traits can be defined outside of factories and used as mixins to compose shared attributes
|
||||
|
||||
```ruby
|
||||
FactoryBot.define do
|
||||
trait :timestamps do
|
||||
created_at { 8.days.ago }
|
||||
updated_at { 4.days.ago }
|
||||
end
|
||||
|
||||
factory :user, traits: [:timestamps] do
|
||||
username { "john_doe" }
|
||||
end
|
||||
|
||||
factory :post do
|
||||
timestamps
|
||||
title { "Traits rock" }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### Using traits
|
||||
|
||||
Traits can also be passed in as a list of symbols when you construct an instance
|
||||
|
|
Loading…
Add table
Reference in a new issue