Adds traits as mixins to 'Getting started' (#1521)

This commit is contained in:
Eike Send 2022-03-08 03:53:51 +01:00 committed by GitHub
parent 1b81d5dc25
commit dc65e786a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 0 deletions

View File

@ -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