1

When I run a task that should be running in the test environment, it is running in development. This includes rake test:(units/functionals/integration) and rake db:test:prepare. A side effect is that it is overwriting my database every time I run tests. I renamed the development database, and now it keeps complaining that it can't find my old database (the one in database.yml). The environment is being set to development in require 'rails' at the top of my application.rb. Am I missing some sort of simple configuration?

Edit: If I run my tests appended with RAILS_ENV=test, they are run in the test environment. However, I can't actually run them because I have pending migrations (all of them). Running rake db:test:prepare RAILS_ENV=test gives me the same pending-migrations error.

Update: The database is migrated, and my tests are going. I still feel like running my tests every time with RAILS_ENV=test isn't the correct way to be doing it

Update: I found that if I change def env in railties to default to 'test', the environment becomes test. That is

def env
  @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
end
Peter Klipfel
  • 4,958
  • 5
  • 29
  • 44

2 Answers2

0

Change this line ENV ||= "test" to ENV = "test" in your spec_helper

If you are using Capybara and have rails server running in development mode then Capybara will use that instead of booting up a test instance.

A fix can be found here:

How to use the test database with Capybara?

Community
  • 1
  • 1
Ravi
  • 166
  • 1
  • 2
  • I'm using Test:Unit, but at the top of my test_helper, I've tried `ENV["RAILS_ENV"] = "test"`, `RAILS_ENV = "test"`, and `ENV = "test"`. None seem to be working. Also, I am not running a server concurrently (I don't think). I ran `top` and didn't see anything that looked like a rails server. Also, I tried the fix on the linked page, and it didn't make any difference – Peter Klipfel Aug 28 '12 at 19:12
0

The solution was to use ENV["RACK_ENV"] instead of the other options that I had tried. I'm still not sure why the other ones didn't work, but I'm going to call this question solved.

So simple...

Peter Klipfel
  • 4,958
  • 5
  • 29
  • 44