Skip to content

Instantly share code, notes, and snippets.

@emarthinsen
Created December 6, 2012 04:01
Show Gist options
  • Save emarthinsen/4221682 to your computer and use it in GitHub Desktop.
Save emarthinsen/4221682 to your computer and use it in GitHub Desktop.
Traits in Associations
(rdb:1) p FactoryGirl.build(:user, :pro, :active)
ArgumentError Exception: Factory not registered: [:subscription, :pro]
(rdb:1) p FactoryGirl.build(:subscription, :pro)
#<Subscription id: nil, user_id: nil, plan: "pro", renew_on: "2012-12-13", charge_on: "2012-12-13", failures: 0, created_at: nil, updated_at: nil>
FactoryGirl.define do
factory :subscription do
plan 'free'
renew_on { 1.week.from_now.to_date }
charge_on { 1.week.from_now.to_date }
failures 0
trait :pro do
plan 'pro'
end
trait :trial do
plan 'trial'
end
end
end
SPEC_DEFAULT_PASSWORD = 'super-secret'
FactoryGirl.define do
factory :user do
account_type 'free'
sequence(:email) {|n| "user_#{n}@example.com" }
password SPEC_DEFAULT_PASSWORD
password_confirmation { password }
listing { create(:listing, :with_images) }
trait :active do
active true
end
trait :pro do
account_type 'pro'
listing { create(:listing, :with_images, image_count: 6) }
credit_card
association :subscription, factory: [:subscription, :pro]
end
trait :trial do
account_type 'trial'
listing { create(:listing, :with_images, image_count: 6) }
credit_card
association :subscription, factory: [:subscription, :trial]
end
end
end
@emarthinsen
Copy link
Author

Line 19 in user_factory.rb is unable to find the factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment