What's the difference between the build and create methods in FactoryGirl?

后端 未结 3 1363
时光取名叫无心
时光取名叫无心 2020-12-13 01:59

The Factory Girl introduction delineates the difference between FactoryGirl.build() and FactoryGirl.create():

# Returns a User inst         


        
3条回答
  •  孤城傲影
    2020-12-13 02:24

    Using FactoryGirl.build(:factory_name) does not persist to the db and does not call save!, so your Active Record validations will not run. This is much faster, but validations might be important.

    Using FactoryGirl.create(:factory_name) will persist to the db and will call Active Record validations. This is obviously slower but can catch validation errors (if you care about them in your tests).

提交回复
热议问题