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

Practical factory_girl

Practical factory_girl

RubyKaigi 2014 Lightning Talks

Kenichi Takahashi

September 19, 2014
Tweet

More Decks by Kenichi Takahashi

Other Decks in Programming

Transcript

  1. /FTUFEGBDUPSZ FactoryGirl.define do factory :user do sequence(:email) {|i| … }

    factory :deactivated_user do deactivated_at { 1.day.ago } end end end
  2. USBJU FactoryGirl.define do factory :user do … ! trait :deactivated

    do deactivated_at { 1.day.ago } end ! factory :deactivated_user, traits: %i(deactivated) end end
  3. %FpOFBENJOVTFS FactoryGirl.define do factory :user do ... factory :admin_user do

    admin true end factory :deactivated_user do ... end end end
  4. %FpOFNPOUIMZCJMMJOHVTFS FactoryGirl.define do factory :user do ... factory :monthly_billing_user do

    billing_started_on { 1.month.ago } end factory :admin_user do ... end factory :deactivated_user do ... end end end
  5. &YBNQMF FactoryGirl.define do factory :user do ... trait :admin do

    admin true end trait :deactivated do deactivated_at { 1.day.ago } end factory :admin_user, traits: %i(admin) factory :admin_deactivated_user, traits: %i(admin deactivated) end end
  6. 'BDUPSZ FactoryGirl.define do factory :user do ... end factory :product

    do ... end factory :order do user product end end
  7. 6TFDBTF describe Order do let(:user_1) { create(:user) } let(:user_2) {

    create(:user) } let(:product_1) { create(:product) } let(:product_2) { create(:product) } context 'user_1 orders product_1' do ... end context 'user_2 orders many products' do ... end end
  8. &YBNQMF describe Organization do let(:organization) { create(:organization) } context 'has

    no member' do ... end context 'has many members' do before do ... end end end
  9. USBJUPSDPOUFYU # use trait describe Organization do let(:org) { create(:organization,

    :foo) } end # use context describe Organization do let(:org) { create(:organization) } context 'foo' do before do org.foo('foo') end end end
  10. "OEpYUVSFT  'BTU  ZBNM PSDTW   4LJQWBMJEBUJPOTBOEDBMMCBDLT see

    “Deep down fixtures” - Prathamesh Sonpatki