I'm testing with rspec a Company
model that has several Addresses
.
I'm using factory girl to produce my test environment, I would like the default Company to have a few addresses registered, while I would like to have a trait for a specific case of a Company without any Address
.
Here's what I'm doing now in the factory:
FactoryGirl.define do
factory :company do
sequence(:name) { |n| "company #{n}" }
description { "description #{name}" }
trait :no_address do
addresses nil
end
after(:create) do |company, evaluator|
create_list(:address, 3, company: company)
end
end
end
how can I make the factory so that when I use the trait no_address
the addresses are not created at all?
thanks,
FactoryGirl's transient attributes might be just what you need:
FactoryGirl.define do
factory :company do
transient do
create_address true
end
sequence(:name) { |n| "company #{n}" }
description { "description #{name}" }
trait :no_address do
transient do
create_address false
end
end
after(:create) do |company, evaluator|
create_list(:address, 3, company: company) if evaluator.create_address
end
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.