I have a pretty short Factory Girl trait that takes parameters and creates a has_many relation. I can call this trait part of another trait to dry out the traits or to facilitate the linking of the attributes together, transferring them to the factories. What I donβt know how to do is how to pass parameters to a characteristic when I call it from another characteristic, or what to do.
eg.
FactoryGirl.define do factory :currency do name Forgery::Currency.description sequence(:short_name) { |sn| "#{Forgery::Currency.code}#{sn}" } symbol '$' end factory :price do full_price { 6000 } discount_price { 3000 } currency subscription end sequence(:base_name) { |sn| "subscription_#{sn}" } factory :product do name { generate(:base_name) } type { "reading" } duration { 14 } trait :reading do type { "reading subscription" } end trait :maths do type { "maths subscription" } end trait :six_month do name { "six_month_" + generate(:base_name) } duration { 183 } end trait :twelve_month do name { "twelve_month_" + generate(:base_name) } duration { 365 } end factory :six_month_reading, traits: [:six_month, :reading] factory :twelve_month_reading, traits: [:twelve_month, :reading] trait :with_price do transient do full_price 6000 discount_price 3000 short_name 'AUD' end after(:create) do |product, evaluator| currency = Currency.find_by(short_name: evaluator.short_name) || create(:currency, short_name: evaluator.short_name) create_list( :price, 1, product: product, currency: currency, full_price: evaluator.full_price, discount_price: evaluator.discount_price ) end end trait :with_aud_price do with_price end trait :with_usd_price do with_price short_name: 'USD' end end end create(:product, :with_aud_price)
ruby-on-rails factory-bot
jim
source share