mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add FactoryGirl Sequences as a ruby til.
This commit is contained in:
@@ -76,6 +76,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
- [Destructuring Arrays In Blocks](ruby/destructuring-arrays-in-blocks.md)
|
||||
- [Disassemble Some Codes](ruby/disassemble-some-codes.md)
|
||||
- [Evaluating One-Off Commands](ruby/evaluating-one-off-commands.md)
|
||||
- [FactoryGirl Sequences](ruby/factory-girl-sequences.md)
|
||||
- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md)
|
||||
- [Limit Split](ruby/limit-split.md)
|
||||
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
||||
|
||||
43
ruby/factory-girl-sequences.md
Normal file
43
ruby/factory-girl-sequences.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# FactoryGirl Sequences
|
||||
|
||||
[FactoryGirl sequences](https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#sequences)
|
||||
are often used inline for unique values such as emails:
|
||||
|
||||
```ruby
|
||||
factory :user do
|
||||
sequence(:email) { |n| "person#{n}@example.com" }
|
||||
end
|
||||
```
|
||||
|
||||
However, a sequence can be defined on its own
|
||||
|
||||
```ruby
|
||||
FactoryGirl.define do
|
||||
sequence :email do |n|
|
||||
"person#{n}@example.com"
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
That means it can be invoked outside the context of a factory
|
||||
|
||||
```ruby
|
||||
> FactoryGirl.generate :email
|
||||
=> "person1@example.com"
|
||||
> FactoryGirl.generate :email
|
||||
=> "person2@example.com"
|
||||
```
|
||||
|
||||
Or it can be used as a shared sequence across multiple factories
|
||||
|
||||
```ruby
|
||||
factory :customer do
|
||||
...
|
||||
email
|
||||
end
|
||||
|
||||
factory :admin do
|
||||
...
|
||||
email
|
||||
end
|
||||
```
|
||||
Reference in New Issue
Block a user