FactoryGirl 有很多特质创造不起作用

FactoryGirl has many trait creation not working

我正在尝试让 FactoryGirl 创建 quiz_question。但它只是在测试数据库环境中创建属于工厂(quiz_campaign),而不是有很多工厂(quiz_question)。

quiz_campaign.rb

FactoryGirl.define do
  trait :with_quiz_questions do
    after_create do |campaign|
      FactoryGirl.create(:quiz_question, quiz_campaign: campaign)
    end      
  end
end

factory :quiz_campaign do 
   subdomain "macy"
end

quiz_question.rb

FactoryGirl.define do
  factory :quiz_question do
    message "What's your question"
  end
end

quiz_home_page.feature

Given(/^there's a subdomain for a quiz campaign$/) do
  @quiz_campaign = create(:quiz_campaign)
end

When(/^I visit the quiz campaign microsite subdomain url$/) do
  visit_with_subdomain microsite_home_path, subdomain: @quiz_campaign.subdomain
end

通过不使用特征解决了它。似乎使用特征会导致比需要的更复杂的解决方案