Rails unit testing doesn't load fixtures

xijo picture xijo · Oct 10, 2009 · Viewed 11.9k times · Source

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!

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?

Answer

Chris picture Chris · Oct 10, 2009

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 fixtures :all isn't be called. Perhaps try putting the call in the initialize method of test_helper.

My test/test_helper.rb looks like this:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  # Add more helper methods to be used by all tests here...
end