I'm trying to test some environment-specific settings (middleware to be specific, but it doesn't seem to matter), but I'm having trouble getting the tests to run in the correct environment. The symptoms:
- If I run
ruby test/unit/my_test.rb
, all is fine because the first thing it does is requiretest/test_helper.rb
, which sets the environment to"test"
and then loads the Rails environment. - If I run
rake test
, the first batch (functionals) run fine, but the second batch (units) fail. The failure is thatENV['RAILS_ENV]
somehow gets unset between batches, thenconfig/environment.rb
sees that none is set and uses the default of"development"
. The environment is correct at the beginning ofconfig/environment.rb
and at the beginning of the configuration block in that file, but wrong by the end. I can tell by using aputs
or by deletingconfig/development.rb
causing it to not find the file. - If I run
rake test:units
, I get the same problem as the second batch ofrake test
(that is, all fail) - If I run
rake test:functionals
, I get the same as forrake test:units
- If I run
rake test RAILS_ENV=test
orrake test:units RAILS_ENV=test
orrake test:functionals RAILS_ENV=test
, all is fine!
One thing I tried that doesn't work at all is the following:
# in Rakefile:
task :set_test_env { ENV['RAILS_ENV'] = 'test' }
Rake::Task[:test].prerequisites.unshift :set_test_env
# and similarly for other test tasks
I even tried creating a separate one of those :set_test_env
tasks for each test task so that I was sure it would get called before each one instead of just once. Still no dice. It does get called, and the environment is correct at the beginning of config/environment.rb
but something goes wrong inside.
I have no calls to set ENV['RAILS_ENV']
in my code anywhere.