factory_botの続き

昨日の日記 でfactory_botの文句を言ったんだけど、心を入れかえて、ドキュメントを読んでみました。

callbackという機能があってそれを使うとやりたいことが出来た。(ヤッター)

FactoryBot.define do
  factory :poduct, class: 'Product' do
    after(:build) do |product|
      product.items = [FactoryBot.build(:low_item), FactoryBot.build(:high_item)]
  end
  
  factory :low_item, class: 'Item'
  factory :high_item, class: 'Item'
end