rspec - Using factory girl to create a trait of has_many relationship -
i'm looking dry test suite. trying create trait represents specific values of has_many relationship on parent factory. ideally these values created separate factory.
i want this:
factory :room trait :bathroom type :bathroom end end factory :house trait :one_bathroom association, :rooms, factory: [:room, :bathroom] end end the above should work if relationship between house , room 1 1. house , room has 1 many relationship, house holds array of rooms. working off example looking create house had array of rooms 1 bathroom.
any ideas?
to create one-to-many relationship this:
factory :room factory :bathroom type :bathroom end factory :bedroom type :bedroom end end factory :house ignore num_bathrooms 0 num_bedrooms 0 end trait :two_bathrooms num_bathrooms 2 end trait :three_bedrooms num_bedrooms 3 end after(:create) |house, evaluator| create_list(:bathroom, evaluator.num_bathrooms, house: house) create_list(:bedroom, evaluator.num_bedrooms, house: house) end end check out last example in associations section of link more details.
Comments
Post a Comment