Tuesday, June 16, 2009

Factory Girl - fixtures replacement for focussed and readable tests

Why Factory Girl:
  1. Fixtures separates the data we are testing with actual behavior
  2. Makes tests brittle and you have to look at fixture files to understand the tests, which is certainly not the intent for the tests
  3. Its painful to have fixtures maintained for each model
  4. You have to keep switching between files to see what fixtures there are, always struggling with dependencies and conflicts
How to get started:
  1. gem install thoughtbot-factory_girl --source http://gems.github.com
  2. Add the following code to your test environment file ---> config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => http://gems.github.com
  3. Add test/factories.rb if using UnitTesting framework or spec/factories.rb if using Rspec framework. It will be automatically loaded if defined at this location
  4. Ryan Bates has an excellent screencast @ http://railscasts.com/episodes/158-factories-not-fixtures
  5. Excellent documentation

No comments: