Updated : August 2011 to work with Factory Girl 2.0.5
It’s been annoying me for some time that when I hit reload! in the Rails console my factories stop working, or point to the wrong class. This wasn’t a real issue until I started using Devise.
Devise uses a mapping between classes and routes, so when a factory built object comes through to Devise after a console reload, or a class redefinition then it will fail. This is commonly the case in development and test environments.
I sat down with a colleague today (thanks Darcy) and we found the appropriate place to reload Factories to have it all work.
This is the code we put into my application.rb :
ActionDispatch::Callbacks.after do # Reload the factories return unless (Rails.env.development? || Rails.env.test?) unless FactoryGirl.factories.blank? # first init will load factories, this should only run on subsequent reloads FactoryGirl.factories.clear FactoryGirl.find_definitions end end
After that everything just worked and I can hit reload! in the console without any issue.