1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00
Files
til/ruby/factory-girl-sequences.md
2015-06-01 08:02:03 -05:00

763 B

FactoryGirl Sequences

FactoryGirl sequences are often used inline for unique values such as emails:

factory :user do
  sequence(:email) { |n| "person#{n}@example.com" }
end

However, a sequence can be defined on its own

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

> FactoryGirl.generate :email
=> "person1@example.com"
> FactoryGirl.generate :email
=> "person2@example.com"

Or it can be used as a shared sequence across multiple factories

factory :customer do
  ...
  email
end

factory :admin do
  ...
  email
end