Fork me on GitHub

October 29, 2010

Configuring Factory Girl Correctly

Post moved to http://log2.kares.org/post/59891243027/configuring-factory-girl-correctly

Factory Girl is a great fixture replacement from thoughtbot. However, sometimes a portion of ugliness might spoil the picture e.g. when the gem is not correctly setup with Bundler (at least in Rails 2.3). A typical gem 'factory_girl' setup seems to be working fine but only until You run some database related tasks such as rake db:reset RAILS_ENV=test. Here's what You might end up with :

Mysql::Error: Table 'SomeApp_test.users' doesn't exist: SHOW FIELDS FROM `users`

This is being caused by the gem initialization hook for Rails, executing Factory.find_definitions while the database is in an inconsistent state.

Seems we'll need to get back to the good old days of require it yourself. But adding a require 'factory_girl' in a test helper is not enough. We need to make sure that the factory definitions are resolved (the initializaiton hook might not execute as the Rails.configuration probably already finished initializing while we're loading factory_girl from a test_helper) :

No comments:

Post a Comment