I would like to use FactoryBot to return trait randomly like that:
FactoryBot.define do
factory :user do
[:active, inactive].sample
trait :active do
active { true }
...
end
trait :inactive do
active { false }
...
end
end
end
To do that:
(1..5).map{ |e| FactoryBot.build(:user) }.map(&:active?)
=> [true, false, false, true, false]
Actually is like that:
FactoryBot.define do
factory :user do
active { [true, false].Sample }
name { "name-#{SecureRandom.uuid}" }
birthday { active == true ? rand(18..99).years.ago - rand(0..365).days.ago : nil }
preferred_contact_method { active == true ? %w(phone email).sample : nil }
activated_at { active == true ? rand(1..200).days.ago : nil }
contact_phone_number { preferred_contact_method == "phone" ? "+33XXXXXXXXX" : nil }
contact_email { preferred_contact_method == "email" ? "toto@tati.com" : nil }
end
end
Is it possible to do that?