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,11 +1,17 @@
--- ---
title: Factory Girl 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}
### Defining factories
```ruby
FactoryGirl.define do FactoryGirl.define do
factory :user do factory :user do
first_name 'John' first_name 'John'
@ -16,55 +22,107 @@ layout: default-ad
sequence(:username) { |n| "user#{n}" } sequence(:username) { |n| "user#{n}" }
end end
end end
```
{: data-line="2"}
# Also available: See: [Defining factories](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Defining_factories)
factory :user, class: 'User'
factory :user, aliases: [:author]
## Using ### Extra options
```ruby
# Custom class names
factory :user, class: 'User' do
···
end
```
```ruby
# Aliases
factory :user, aliases: [:author] do
···
end
```
### Using
```ruby
FactoryGirl.build(:user) FactoryGirl.build(:user)
```
```ruby
build(:user) # not saved build(:user) # not saved
create(:user) # saved create(:user) # saved
attributes_for(:user) # hash attributes_for(:user) # hash
build_stubbed(:user) # stubbed out attributes build_stubbed(:user) # stubbed out attributes
```
```ruby
# With options:
build(:user, name: 'John') build(:user, name: 'John')
```
```ruby
# List:
create_list(:user, 3) create_list(:user, 3)
build_list(:user, 3) build_list(:user, 3)
```
## Associations ## Associations
### Defining
```ruby
factory :post do factory :post do
association :author, factory: :user association :author, factory: :user
association :author, factory: [:user, :admin] association :author, factory: [:user, :admin]
end
```
{: data-line="2,3"}
or:
```ruby
factory :post do
author # assumes there's a factory :author author # assumes there's a factory :author
end end
```
### After-create hooks ### After-create hooks
```ruby
factory :post do factory :post do
after :create do |post| after :create do |post|
create :theme, post: post # has_one create :theme, post: post # has_one
create_list :comment, 3, post: post # has_many create_list :comment, 3, post: post # has_many
end end
end end
```
{: data-line="2"}
## Traits ## Other features
{: .-three-column}
### Traits
```ruby
factory :user do factory :user do
trait :admin do trait :admin do
admin true admin true
end end
end end
```
{: data-line="2,3,4"}
```ruby
create :user, :admin create :user, :admin
```
## Nested factories Traits allow you to group attributes together.
See: [Traits](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Traits)
### Nested factories
```ruby
factory :user do factory :user do
first_name 'John' first_name 'John'
@ -72,13 +130,39 @@ layout: default-ad
first_name { FFaker::Name.first_name } first_name { FFaker::Name.first_name }
end end
end end
```
{: data-line="4,5,6"}
# create :sample_user ```ruby
create :sample_user
```
# Also: factory :sample_user, parent: :user See: [Inheritance](http://www.rubydoc.info/gems/factory_girl/file/GETTING_STARTED.md#Inheritance)
## Options (transients) ### Sub-factories
```ruby
factory :user do
···
end
```
```ruby
factory :sample_user, parent: :user do
first_name { FFaker::Name.first_name }
end
```
{: data-line="1"}
```ruby
create :sample_user
```
Works the same as nested factories.
### Options (transients)
```ruby
factory :user do factory :user do
transient do transient do
upcased true upcased true
@ -88,16 +172,27 @@ layout: default-ad
user.name.upcase! if options.upcased user.name.upcase! if options.upcased
end end
end end
```
{: data-line="2,3,4"}
```ruby
create(user, upcased: true) create(user, upcased: true)
```
## Paths 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)
test/factories.rb ### Paths
spec/factories.rb
test/factories/*.rb * test/factories.rb
spec/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>