I think RAILS_ENV is getting set incorrectly, but I don't know exactly how to find out where.
At the bottom of Rails::Initializer.run do |config| in environment.rb, RAILS_ENV is the correct value (staging, development, production, etc).
Just below the initializer block, RAILS_ENV has changed to 'test'.
Any ideas?