kylejginavan / factory_data_preloader

Preload data created by factory girl (or any other Rails fixtures alternative) so that you can use Rails' built-in test transactions.
https://github.com/myronmarston/factory_data_preloader
33 stars 3 forks source link

error running rake test after removing all fixtures files #1

Open benhutton opened 14 years ago

benhutton commented 14 years ago

Hey,

I just tracked down a bug in our system that prevented us from running rake test (could still run the individual rake test:units, test:functionals, etc.).

I had gone through and completely removed my fixtures (such that there were no files in the fixtures folder). In the rails_core_ext.rb file, you put the FactoryData.delete_preload_data! function into rails' delete_existing_fixtures function. This in turn gets called at line 511 in activerecord-2.3.5/lib/active_record/fixtures.rb, which is inside the "unless table_names_to_fetch.empty?" block, which will never be executed if there are no fixtures in the test/fixtures directory. This means that the factories would never get unloaded, so the next time they were loaded, I would get all sorts of validation errors ("username already exists", etc.). Does that make sense?

My solution is to just create a blank fixture file (profiles.yml) in the fixtures directory, which causes the loop to be executed, which allows the factories to be unloaded, which allows me to run rake test (and rake rcov).

Maybe my solution is good enough. Just wanted to document it here in case somebody else has the same problem.

myronmarston commented 14 years ago

The app I originally developed this for uses both fixtures (for static bootstrap data) and factories, so we didn't have this problem. For now, having the empty fixtures file is probably the best solution.

Long term, we may want to investigate finding other ways to hook into the test setup/teardown lifecycle to do the the preloading/deleting. The way I did it was the simplest way I could find with a bit of investigation of the code for fixtures.

If you find a simpler/better way that doesn't have this problem, feel free to fork and submit a patch back :).