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

Last updated