0

I start with a test database containing the schema but with no data in the tables. I run a test like so

cd test/
ruby unit/directive_test.rb

I get failures indicating that the code found no data in the data tables. However, I look at the tables after running that test and the data is now in the the table. In fact, if I immediately run the test again I get no failures.

So it appears that the fixture is being loaded into the table too late for one of my modules to find it.

  1. When are the fixtures loaded? After or before the app/model/*.rb files are executed?
  2. If it is after the models are executed is there a way to delay the loading?

This issue is also relevant when running rake test:units since that task clears the test data after it finished.

rlandster
  • 7,294
  • 14
  • 58
  • 96
  • Your fixtures should be under `test/fixtures/model_name.yml` unless they are being populated by migrations. It sounds to me like you're breaking conventions and that is why the tests are breaking. Check out the testing guide and see if things are set up the right way: http://guides.rubyonrails.org/testing.html – Joseph Yaduvanshi May 06 '10 at 17:30
  • All my fixtures are under `test/fixtures/` so I do not think that is the problem. The issue seems to be one of timing. My guess is that for a fixture to be loaded properly the model that the fixture uses must first be loaded forcing the fixtures to be loaded last. In one of my models I attempt to read data into a class variable outside of any methods hence the problem: no data exists to read (yet). – rlandster May 06 '10 at 19:44

1 Answers1

0

first of all see this thread and see if it can help you.

if you run the rail task rake test:units it will for sure load all fixtures before you run your code. if you are running just the test, and your unit test has no reference to the test_help.rb probably it is not loading the fixtures. You should try to run it through the rake tasks.

Another tip that i give you is that you forget the fixtures and use factories (here i recommend factory_girl). It takes sometime to get used, but it worth. Fixtures are too hard to manage, update and etc.

there is another post explaing little about the concept behind factories.

Community
  • 1
  • 1
VP.
  • 5,122
  • 6
  • 46
  • 71