The Factory Girl introduction delineates the difference between FactoryGirl.build()
and FactoryGirl.create()
:
# Returns a User inst
FactoryGirl.create()
will create new object and associations (if the factory has any) for it. They will all be persisted in a database. Also, it will trigger both model and database validations.
Callbacks after(:build)
and after(:create)
will be called after the factory is saved. Also before(:create)
will be called before the factory is saved.
FactoryGirl.build()
won't save an object, but will still make requests to a database if the factory has associations. It will trigger validations only for associated objects.
Callback after(:build)
will be called after the factory is built.
Note that in most cases when testing models are best to use build_stubbed
for better performance. Read more about it here.