Why do test:units and test:functionals insist on running in development environment?
To give you a full answer I would have to take a look at the code, but I'll try to give you some clues that might be helpful.
First of all rake test
and all the other variants (test:units
, test:functionals
etc.) work as follows
- Rake process is invoked and task
test
is executed in the current environment (which is development by default), that's whydevelopment.rb
is always read. - The Rake task invokes the test loader in a separate child process (you can verify this with
ps
orpstree
), this is where thetest_helper.rb
is sourced and environment is set to test.
When you run ruby test/unit/my_test.rb
the first step is skipped, so it looks like the problem lies there. Maybe you do something in development.rb
that has side effects for the subprocess?
I pretty much always want to force my tests to run themselves and their prerequisites in the "test" environment, especially when ENV['RAILS_ENV'] is set to any of the common defaults (to avoid catastrophic accidents), but I also want to be able to run tests on, say, an environment named "v_2_0_maint_test" or something like that by calling rake test:units RAILS_ENV=v_2_0_maint_test
on the command line.
So I have a test_tasks.rake file that prepends a prerequisite onto each of the test tasks that I'm interested in. Since this prerequisite is prepended, any other prerequisites (e.g. db:test:prepare, db:fixtures:load) run in the same environment. This claims the virtue of affecting only the tests you want to affect, and their prerequisites.
namespace :test do |n| [ n[:units], n[:functionals], n[:integration] ].each do |t| t.prerequisites.unshift(:set_test_env_dammit) end task :set_test_env_dammit do |t| if [ nil, "", "development", "staging", "production" ].index ENV['RAILS_ENV'] RAILS_ENV = "test" end endend
At the top of the test_helper.rb file I have the code
ENV["RAILS_ENV"] = "test"
If you do not have that line then the system problem would run in the default environment (i.e. development).