factory_girl: update

This commit is contained in:
Rico Sta. Cruz 2017-09-01 04:59:41 +08:00
parent 744ee80feb
commit ccd53663c0
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 160 additions and 65 deletions

View File

@ -1,103 +1,198 @@
--- ---
title: FactoryGirl title: Factory Girl
category: Ruby libraries category: Ruby libraries
layout: default-ad layout: 2017/sheet
weight: -3
updated: 2017-09-01
--- ---
## Factories ## Factories
{: .-three-column}
FactoryGirl.define do ### Defining factories
factory :user do
first_name 'John'
last_name 'Doe'
birthdate { 21.years.ago }
admin false
sequence(:username) { |n| "user#{n}" } ```ruby
end FactoryGirl.define do
end factory :user do
first_name 'John'
last_name 'Doe'
birthdate { 21.years.ago }
admin false
# Also available: sequence(:username) { |n| "user#{n}" }
factory :user, class: 'User' end
factory :user, aliases: [:author] end
```
{: data-line="2"}
## Using See: [Defining factories](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Defining_factories)
FactoryGirl.build(:user) ### Extra options
build(:user) # not saved ```ruby
create(:user) # saved # Custom class names
attributes_for(:user) # hash factory :user, class: 'User' do
build_stubbed(:user) # stubbed out attributes ···
end
```
build(:user, name: 'John') ```ruby
# Aliases
factory :user, aliases: [:author] do
···
end
```
create_list(:user, 3) ### Using
build_list(:user, 3)
```ruby
FactoryGirl.build(:user)
```
```ruby
build(:user) # not saved
create(:user) # saved
attributes_for(:user) # hash
build_stubbed(:user) # stubbed out attributes
```
```ruby
# With options:
build(:user, name: 'John')
```
```ruby
# List:
create_list(:user, 3)
build_list(:user, 3)
```
## Associations ## Associations
factory :post do ### Defining
association :author, factory: :user
association :author, factory: [:user, :admin]
author # assumes there's a factory :author ```ruby
end factory :post do
association :author, factory: :user
association :author, factory: [:user, :admin]
end
```
{: data-line="2,3"}
or:
```ruby
factory :post do
author # assumes there's a factory :author
end
```
### After-create hooks ### After-create hooks
factory :post do ```ruby
after :create do |post| factory :post do
create :theme, post: post # has_one after :create do |post|
create_list :comment, 3, post: post # has_many create :theme, post: post # has_one
end create_list :comment, 3, post: post # has_many
end end
end
```
{: data-line="2"}
## Traits ## Other features
{: .-three-column}
factory :user do ### Traits
trait :admin do
admin true
end
end
create :user, :admin ```ruby
factory :user do
trait :admin do
admin true
end
end
```
{: data-line="2,3,4"}
## Nested factories ```ruby
create :user, :admin
```
factory :user do Traits allow you to group attributes together.
first_name 'John' See: [Traits](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Traits)
factory :sample_user do ### Nested factories
first_name { FFaker::Name.first_name }
end
end
# create :sample_user ```ruby
factory :user do
first_name 'John'
# Also: factory :sample_user, parent: :user factory :sample_user do
first_name { FFaker::Name.first_name }
end
end
```
{: data-line="4,5,6"}
## Options (transients) ```ruby
create :sample_user
```
factory :user do See: [Inheritance](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Inheritance)
transient do
upcased true
end
after :create do |user, options| ### Sub-factories
user.name.upcase! if options.upcased
end
end
create(user, upcased: true) ```ruby
factory :user do
···
end
```
## Paths ```ruby
factory :sample_user, parent: :user do
first_name { FFaker::Name.first_name }
end
```
{: data-line="1"}
test/factories.rb ```ruby
spec/factories.rb create :sample_user
test/factories/*.rb ```
spec/factories/*.rb
Works the same as nested factories.
### Options (transients)
```ruby
factory :user do
transient do
upcased true
end
after :create do |user, options|
user.name.upcase! if options.upcased
end
end
```
{: data-line="2,3,4"}
```ruby
create(user, upcased: true)
```
Transient attributes will not get passed to the model, but will be available in after-create hooks.
See: [Transient attributes](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Transient_Attributes)
### Paths
* test/factories.rb
* spec/factories.rb
* test/factories/*.rb
* spec/factories/*.rb
Place your factories in these locations.
{: .-setup}
## See also ## See also
{: .-one-column}
* <http://rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md> * <http://rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md>