Populating an association with children in factory_girl

前端 未结 5 1961
没有蜡笔的小新
没有蜡笔的小新 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:58

    You can use the association method both ways:

    Factory.define :foo do |f|
      # ...
      f.association :bar
    end
    

    If that won't work, you can associate them manually using a callback. Here's an example from one of my apps:

    Factory.define :live_raid do |raid|
    end
    
    Factory.define :live_raid_with_attendee, :parent => :live_raid do |raid|
      raid.after_create { |r| Factory(:live_attendee, :live_raid => r) }
    end
    

提交回复
热议问题