test succeed only when run separately

But run whith rake everything fails :

rake test:units


How does it fail ? What is in constant_cache.rb ?


So what exactly happens when you run rake ?


Frederick Cheung wrote:

Frederick Cheung wrote:
>> Frederick Cheung wrote:

>> ... all the instance of type StatusAction become constants called

>> well the strangest is that is contant_cache seems not to be correctly
>> run from rake...

> So what exactly happens when you run rake ?

> Fred

When I run ruby unit/status_test.rb : everything is fine, constants are
all present

When I run rake test:units : no constant is set, as if constant_cache
hasn't been run

Did you stick a breakpoint in constant_cache ? I rather suspect that
it is running but that the particular sequence of actions that takes
place means that it happens before fixtures have been loaded.
When you run rake the test database is recreated from scratch and so
starts of empty (and then fixtures are loaded).
When you run a single test file then the test database isn't recreated
and so the fixtures are already there.

Your setup method probably runs after fixtures have been loaded, but
all it does is call require and that file was already required earlier
on when rails loaded your initializers so those requires may well be


Frederick Cheung wrote:

Thanks Fred for your help,

I guess you're right, do you know a nice way to force constant_cache to
be run before each test ?

Otherwise I think i'll try to prevent rake to prepare db before running
tests, but i'm not sure this is an optimal solution to my pb.

it's not actually the running the tests that does it, it's when the
model is loaded (since that is when cache_constants is called). I'm
not sure you current setup method does anything since (best case
scenario) it causes a file to be loaded that adds some methods to
activerecord classes (but doesn't actually call them). You could try
calling Status.cache_constants from your setup method.


I had a very similar problem recently and solved it by changing my
constants to class variables of the appropriate models, then in the
accessor function for each variable, tested for nil and read from db
if necessary. In fact if nil I read the whole set of variables for
that model. I have decided that using class variables is actually
nicer as it locates the 'constants' with the appropriate model.


Frederick Cheung wrote: