shimgo
7/19/2019 - 9:09 AM

factory_botで引数を受け取って処理する

FactoryBot.define do
  factory :user do
    transient do
      skills_count { 1 }
    end

    name { "foo" }
    skills do
      types = [:java, :go, :python]
      (0..(skills_count - 1)).map do |i|
        build(:skill, types[i % types.size])
      end
    end
  end
  
  factory :skill do
    trait :java do
      skill_name: { "java" }
    end
    ...
  end
end

FactoryBot.build(:user, skills_count: 3)