views:

710

answers:

6

Hello together,

rake test:units fails in my current application, because the needed data of the fixtures is missing.

If I'm loading the fixtures manually via rake db:fixtures:load RAILS_ENV=test the unit tests are working, but rake purges the test database.

My test_helper includes fixtures :all and my tests are inheriting from it - but the fixtures are simply not loading.

I'm kind of clueless at the moment and could really need some help!

Thanks and bye,

Joe

edit: I've tried a lot and I think it has to do with some environment settings or plugins used in this project. Does anyone know where to read about which files are loaded for the testing environment?

A: 

Not sure if you already did this, but export the test data from the database to the yml files in test/fixtures using a plugin like ar_fixtures

Jen
Hmm, the problem is to get data from the fixtures into the database, I think ar_fixtures does the other way round! :)
Joe
oh oops :) sorry, misread the question
Jen
A: 

Put the call to fixtures :all in your test class, not the super class (test_helper). My guess is that initialization of the super class isn't working the way you're expecting and that fixtues :all isn't be called. Perhaps try putting the call in the initialize method of test_helper.

Chris
Hi cstump, I tried to put `fixtures :all` directly in my test class, but it didn't work. Seems like the call hasn't any effect - but I just can't image why!
Joe
+1  A: 

I finally found the problem, although the solutions is kind of hacky.

One plugin is relying that there is some data in the database, at least one row. So what happened was:

  1. rake loads database schema
  2. rake tries to load environment
  3. environment includes plugin
  4. plugin loading fails because of missing at least one row
  5. no fixtures are loaded

The hacky solution is: put the needed data directly into schema and not into a fixtures, because it's loaded to late.

I'll search for a more convenient solution and will update this answer if I found one.

Joe
Plugins shouldn't do stuff like that. The best thing to do is fix the plugin and send them a pull request on github.
Sarah Mei
I fixed the issue within the plugin and will submit a patch soon. ;)
Joe
A: 

BTW, I'm stuck in the exact same spot. I'm trying to run an integration test, that test calls fixtures :all, and the fixtures never load. I looked in environment.rb and environment/test.rb to see if either of them were calling a plugin that seemed like it would depend on database data, but I didn't find anything.

I'm basically stuck - if anybody has other ideas (or maybe a way of finding the errant plugin), it would be appreciated!

scottru
Hi Scot, maybe you can list your plugins here so I can have a look at it if there are matchings! Do you get any error messages?
Joe
Thanks, Joe - no error messages.The only gems that are loaded at startup (in environment.rb) are geokit, hpricot, will-paginate.Plugins in vendor/plugins (are those required on load as well? They aren't called out individually) are acts_as_list,acts_as_taggable_on_steroids,acts_as_tree,geokit_rails,hoptoad_notifier,mysql_bigint,rails-footnotes,records_sequence,restful_authentication
scottru
A: 

Another approach is to write your own custom rake task for testing.

For example:


task :test_units do

  RAILS_ENV = 'test' # Force the environment to test

  puts "Recreate the test database"
  Rake::Task['db:test:prepare'].invoke

  puts "Seed the database with fixtures"
  Rake::Task['db:fixtures:load'].invoke

  puts "Executing Unit Tests"
  Rake::Task['test:units'].prerequisites.clear 
  Rake::Task['test:units'].invoke
end


Terence
A: 

I had the same problem. Or rather, the problem was that my fixtures were not current with the database schema. Instead of throwing an exception, rails just used the test database as a fallback solution. Most iffy.

thomax