Factory girl &Faker part II
I think there is only one more thing left for me to say about factory girl, how to handle relations, well it's pretty simple then I thought.
For any(one/many) to many relation just use the following
FactoryGirl.define do factory :channel do name {Faker::Name.name} cluster_discount 0 after(:build) do |channel| channel.push_all(:target_audiences,TargetAudience.all.to_a) end end end
class TargetAudienceFactory FactoryGirl.define do factory :target_audience do name {Faker::Name.last_name} end end end
As you can see I'm just using the after build hook and push as many target audiences as i wish, in this case i add them all.
In any to one you just need to provide the factory name to the object
class TargetAudienceFactory FactoryGirl.define do factory :target_audience do name {Faker::Name.last_name} short_name {name} channel end end end
And that's all there is to know I guess











