Populating an association with children in factory_girl

前端 未结 5 1958
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 17:16

I have a model Foo that has_many \'Bar\'. I have a factory_girl factory for each of these objects. The factory for Bar has an association to Foo; it will instan

5条回答
  •  [愿得一人]
    2020-12-12 17:43

    Using factory_girl-4.5.0, create n child objects in a parent object factory

    FactoryGirl.define do
      factory :foo do
        name "test"        
    
        after(:build) do |instance|
          n.times { instance.bars << FactoryGirl.create(:bar) }          
        end
      end
    end
    

提交回复
热议问题