factory girl in 7 minutes!
TRANSCRIPT
f.association=>
factory f.name f.owner f.description f. f.published f.publish_date DateTime.now
GETTING STARTED WITH
IN 7 MINUTES!FACTORY GIRL
FactoryGirl.define do factory :user do first_name "Luis Alfredo" last_name "Lorenzo" works_at "mxabierto" twitter "@babasbot" endend
WHAT’S FACTORY GIRL?
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
A REPLACEMENT FOR FIXTURES
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
PRO-TIP DO NOT USE FIXTURES :)
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
HOW TO DEFINE FACTORIES ?
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
HOW TO USE FACTORIES?
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
4 WAYS TO GO
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
1 CREATE
character = create(:character)# => #<Character id: 1, first_name: "Jon", last_name: "Snow">
character.persisted?# => true
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
2 BUILD
user = build(:character)# => #<Character id: nil, first_name: "Jon", last_name: "Snow">
character.persisted?# => false
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
3 BUILD_STUBBED
character = build_stubbed(:character)# => #<Character id: 1001, first_name: "Jon", last_name: "Snow">
character.persisted?# => true
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
4 ATTRIBUTES_FOR
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
FACTORIES IN ACTION!
describe Character do describe "#full_name" do it "is composed of first and last name" do user = create(:character, first_name: "Jon", last_name: "Snow") user.full_name.should eql("Jon Snow") end endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
SOME AWESOME STUFF
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
OVERRIDE ATTRIBUTES
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
LAZY ATTRIBUTES
FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" date_of_birth { 20.years.ago } endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
DEPENDENT ATTRIBUTES
FactoryGirl.define do factory :character do first_name "jon" last_name "snow" email { "#{first_name}.#{last_name}@nightswatch.com" } endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
TRANSIENT ATTRIBUTES
FactoryGirl.define do factory :character do ignore do broke_bows false end
name "Jon Snow"
after_create do |character, instance| character.revoke_nights_watch! if instance.broke_bows? end endend
create(:character, broke_bows: true)
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
CALLBACKS
FactoryGirl.define do factory :character do
after_create { |character| character.give_direwolf! } # also: after_build, after_stub, before_create endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
INHERITANCE
FactoryGirl.define do factory :character do
first_name "Jon" last_name "Snow"
factory :deceased_character do deceased_at { Date.today } deceased true end endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
ASSOCIATIONS
FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" animal end
factory :animal do name "Ghost" species "Direwolf" endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
ASSOCIATION ALIASES
FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" direwolf end
factory :animal, aliases: [:direwolf] do name "Ghost" species "Direwolf" endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
SEQUENCES
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
TRAITS
FactoryGirl.define do factory :character do
on_nights_watch false
trait(:nights_watch) { on_nights_watch(true) } trait(:has_direwolf) { direwolf }
factory :jon_snow, traits %i(nights_watch has_direwolf) endend
factory f.csv_file File.spec/fixtures/files/inventory.csv" f.association=>
factory f.name f.owner f.description f. f.published
< EOF >