If I understand you correctly, you want to do this, but with the syntax syntax:
booking_item = Factory(:booking_item, :people => 4) booking = Factory(:booking, :booking_item => booking_item)
Because of this, you can shorten it as follows:
def with_assocs factory, assocs_hashes = {}, attrs = {} assoc_models = Hash[ assocs_hash.map { |k, v| [k, Factory(k, v)] } ] Factory factory, attrs.merge(assoc_models) end
And use like this:
@booking = with_assocs :booking, :booking_item => {:people => 3} @booking.should be_valid
In the active_factory plugin with similar factory definitions, it will look like this:
models { booking - booking_item(:people => 3) } booking.should be_valid
Unfortunately, I have not implemented integration with factory_girl yet. Although if you are interested in any input, very welcome.
source share