Skip to content

Instantly share code, notes, and snippets.

@suchov
Created April 12, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suchov/0ffddf1ae85bcd16c716a0e30b8a6119 to your computer and use it in GitHub Desktop.
Save suchov/0ffddf1ae85bcd16c716a0e30b8a6119 to your computer and use it in GitHub Desktop.
Persisting to the database takes far longer than initializing objects in memory, and
while we’re talking fractions of a second, each of these round trips to the database
adds up when running your entire suite.
When you initialize new objects, try to do so with the least overhead. Depending
on what you need, you should choose your initialization method in this order:
Object.new - initializes the object without FactoryGirl. Use this when you
don’t care about any validations or default values.
FactoryGirl.build_stubbed(:object)-initializestheobjectwithFactoryGirl,
setting up default values and associates records using the build_stubbed
method. Nothing is persisted to the database.
FactoryGirl.build(:object) - initializes the object with FactoryGirl, setting
up default values and persisting associated records with create.
FactoryGirl.create(:object) - initializes and persists the object with Fac-
toryGirl, setting up default values and persisting associated records with
create.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment