Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting started with factory_girl in 7 minutes

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Getting started with factory_girl in 7 minutes

Avatar for Luis Alfredo Lorenzo

Luis Alfredo Lorenzo

June 25, 2015

More Decks by Luis Alfredo Lorenzo

Other Decks in Programming

Transcript

  1. character = create(:character) # => #<Character id: 1, first_name: "Jon",

    last_name: "Snow"> character.persisted? # => true
  2. user = build(:character) # => #<Character id: nil, first_name: "Jon",

    last_name: "Snow"> character.persisted? # => false
  3. 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 end end
  4. FactoryGirl.define do factory :character do first_name "jon" last_name "snow" email

    { "#{first_name}.#{last_name}@nightswatch.com" } end end
  5. 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 end end create(:character, broke_bows: true)
  6. FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" factory

    :deceased_character do deceased_at { Date.today } deceased true end end end
  7. FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" animal

    end factory :animal do name "Ghost" species "Direwolf" end end
  8. FactoryGirl.define do factory :character do first_name "Jon" last_name "Snow" direwolf

    end factory :animal, aliases: [:direwolf] do name "Ghost" species "Direwolf" end end
  9. 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) end end