FactoryGirl: attributes_for not giving me associated attributes

前端 未结 7 629
再見小時候
再見小時候 2020-12-08 14:08

I have a Code model factory like this:

Factory.define :code do |f|
    f.value \"code\"
    f.association :code_type
    f.association(:codeable, :factory =&         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 14:38

    Here's another way. You probably want to omit the id, created_at and updated_at attributes.

    FactoryGirl.build(:car).attributes.except('id', 'created_at', 'updated_at').symbolize_keys

    Limitations:

    • It does not generate attributes for HMT and HABTM associations (as these associations are stored in a join table, not an actual attribute).
    • Association strategy in the factory must be create, as in association :user, strategy: :create. This strategy can make your factory very slow if you don't use it wisely.

提交回复
热议问题