Daniel Hahn

Factory Girls secret savings

We like to use FactoryGirl for our test setup. Unlike Rails' fixtures, it will give you an individual setup for each tests, and thus preventing that tests have side effects on other tests. However, saving objects on each tests comes with a performance penalty.

For your specs, however, you'll only need to call Factory.build in many cases, creating an new object without saving it to the database. Only that if you have a factory like the following, the associated objects will always be saved:


Factory.define :project do |f|
  f.association :carrier, :factory => :organisation
end

That is, even if you call Factory.build(:project), Factory Girl will internally call Factory.create(:organisation), saving the associated object. If you don't want that behaviour in your tests, build your objects "manually" in the tests.

This project is maintained by averell23