Ruby on rails 是否可以在工厂(FactoryGirl)中使用if语句?
我的工厂中有两个特性,我希望在创建对象时包含其中一个特性,而不会默认为一个(因此随机选择该特性)。以下是我正在做的:Ruby on rails 是否可以在工厂(FactoryGirl)中使用if语句?,ruby-on-rails,conditional,factory-bot,Ruby On Rails,Conditional,Factory Bot,我的工厂中有两个特性,我希望在创建对象时包含其中一个特性,而不会默认为一个(因此随机选择该特性)。以下是我正在做的: FactoryGirl.define do factory :follow_up do first_name { Faker::Name.first_name } last_name { Faker::Name.last_name } phone { Faker::PhoneNumber.cell_phone.
FactoryGirl.define do
factory :follow_up do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
phone { Faker::PhoneNumber.cell_phone.gsub(/[^\d]/, '').gsub(/^1/, '2')[0..9] }
email { Faker::Internet.email }
email_preferred true
consent false
if [1, 2].sample == 1
by_referral
else
by_provider
end
trait :by_referral do
association :hospital
association :referral
source { FollowUp::REFERRAL}
end
trait :by_provider do
association :provider
source { FollowUp::PROVIDER }
end
end
end
然而,它似乎忽略了if语句并直接转到by_提供程序trait。有人知道我会怎么做吗?使用忽略块
FactoryGirl.define do
factory :follow_up do
text "text"
author "Jim"
ignore do
if x == 1
by_referral
else
...
end
end
end
end
这似乎是不受欢迎的行为。您的测试套件应该在每次运行时做完全相同的事情,并且不应该有任何随机性。如果有的话,您可能需要在这里进行两个单独的测试,测试逻辑的每个分支。