Skip to content

Latest commit

 

History

History
60 lines (43 loc) · 1.08 KB

factory_girl.md

File metadata and controls

60 lines (43 loc) · 1.08 KB
title layout
FactoryGirl
default

Paths

test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb

Defining stuff

FactoryGirl.define do
  factory ...
end

Factories

# This will guess the User class
factory :user do
  first_name "John"
  last_name  { %w[Doe Smith Doyle].shuffle }
  admin false

  # Sequences
  sequence(:username) { |n| "user#{n}" }

  # Associations
  association :author
  association :author, factory: user, last_name: "Ho"
  author

  # Traits
  trait :admin do
    admin true
  end

  after :create do |user, evaluator| ... end
  after :build
end

factory :user, aliases: [:author, :commenter] do ... end
factory :admin_user, parent: :user do .. end

Using

FactoryGirl.build(:user)

build(:user)          # not saved
create(:user)         # saved
attributes_for(:user) # hash
build_stubbed(:user)  # stubbed out attributes

build(:user, name: "John")

create_list(:user, 3)
build_list(:user, 3)