开发者

Using Factory Girl to create a factory for Thread and Thread Participations

can someone provide an example or point me to where I can learn how to do Factory Girl nested model associations?

A Thread has to have at least one ThreadParticipat开发者_Go百科ion

Right now I have my thread in factories.rb as follows:

Factory.define :thread do |thread|
  thread.title             "mythread"
end

How do I then create a ThreadParticipation?

Thanks


The Getting Started file in the factory_girl source has info on associations.

Associated instances can be generated by using the association method when defining a lazy attribute:

factory :post do
  # ...
  author 
end

You can also specify a different factory or override attributes:

factory :post do
  # ...
  association :author, :factory => :user, :last_name => 'Writely'
end

So, in your instance, I would imagine something like this would do:

Factory.define :thread do |thread|
  thread.title "mythread"
  thread.thread_participation
end

Factory.define :thread_participation do |ppn|
  ppn.attribute "value"
end

If you're using a collection instead of a has_one/belongs_to association, you can create an array as such:

Factory.define :thread do |thread|
  thread.title "mythread"
  thread.thread_participations { |a| [a.association(:thread_participation)] }
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜