factory girl in 7 minutes!

47
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

Upload: luis-lorenzo

Post on 13-Apr-2017

345 views

Category:

Technology


0 download

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

jon: first_name: Jon last_name: Snow

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 ?

FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" endend

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

attrs = attributes_for(:character)# => { :first_name => "Jon", :last_name => "Snow" }

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

character = build(:character, first_name: "Tyrion")character.first_name# => "Tyrion"

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

FactoryGirl.define do factory :crow do sequence :email do |n| "crow#{n}@nightswatch.com" end endend

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 >

FactoryGirl.define do factory :babasbot do name "Luis Alfredo Lorenzo" github "/babasbot" twitter "@babasbot" endend