Multiple Test Environments in Rails

Multiple test enviorments allow for developers with different DB settings, gem desires to be able to setup and run the same test suite independently of one another.

Create the file config/enviroments/jordan_test.rd and copy into it the contents of config/enviroments/test.rb Via command line from application root: cp config/environments/test.rb config/environments/jordan_test.rb

in config/database.yml add your DB settings (assumes using Postgres)

database.yml
1
2
3
4
5
6
7
jordan_test:
  adapter: postgresql
  encoding: utf8
  host: localhost
  database: [project_name]_jordan_test
  username:
  password:

Create the new database

1
rake db:create:all

add environment to any :test specific gems or gem groups in your gem file

Gemfile
1
2
3
group :test, :jordan_test do
    #blaaaaa........
end

or make a bundler group in application.rb so others envs can be added easier.

config/application.rb
1
2
3
4
5
6
if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test steve cronin sexyjordan_test), :test_envs => %w(test sexyjordan_test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

Usual run rake db:test:prepare or db:test:clone instead possible to run rake db:migrate RAILS_ENV=jordan_test perhaps make an alias for this in your .bash_profile