FactoryGirl: why does attributes_for omit some attributes?

前端 未结 5 1295
借酒劲吻你
借酒劲吻你 2020-12-03 10:57

I want to use FactoryGirl.attributes_for in controller testing, as in:

it \"raise error creating a new PremiseGroup for this user\" do
  expect {
    post :c         


        
5条回答
  •  攒了一身酷
    2020-12-03 11:39

    Here is another way:

    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.

提交回复
热议问题