是否可以在FactoryGirl中定義默認特質?如果我這樣定義一個工廠(其中兩個question_response belongs_to的問題):在FactoryGirl中使用'默認'特性以避免不必要的關聯創建
factory :question_response do
question
work_history
trait :open do
question { FactoryGirl.create :question, question_type: 'open' }
end
end
當我做FactoryGirl.create :question_response, :open
它會首先創建一個默認問題,然後創建另一個特質,這是不必要的操作中。
理想我想這樣做:
factory :question_response do
work_history
trait :default do
question { FactoryGirl.create :question, question_type: 'yes_no' }
end
trait :open do
question { FactoryGirl.create :question, question_type: 'open' }
end
end
,然後做FactoryGirl.create :question
將使用默認的特質,但它似乎並不可能。
謝謝,你說得很對!我剛剛證實了這一點。我不知道這是最近添加到工廠女孩... –