FactoryGirl and polymorphic associations

后端 未结 6 1475
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 14:18

The design

I have a User model that belongs to a profile through a polymorphic association. The reason I chose this design can be found here. To summarize, there a

6条回答
  •  温柔的废话
    2020-12-23 15:20

    Factory_Girl callbacks would make life much easier. How about something like this?

    Factory.define :user do |user|
      #attributes for user
    end
    
    Factory.define :artist do |artist|
      #attributes for artist
      artist.after_create {|a| Factory(:user, :profile => a)}
    end
    
    Factory.define :musician do |musician|
      #attributes for musician
      musician.after_create {|m| Factory(:user, :profile => m)}
    end
    

提交回复
热议问题