Dan Rosenstark
Dan Rosenstark

Reputation: 69757

Set RAILS_ENV for test rake tasks

I've done this patch to my test_helper.rb

ENV["RAILS_ENV"] = ENV["RAILS_ENV_TEST"] || "test"

This works in that I can run

RAILS_ENV_TEST=test_dan ruby -Itest test/unit/post_test.rb

but I want to be able to run all kinds of test things, including rake db:test:clone but not using the environment test. How can I do this?

Upvotes: 0

Views: 2417

Answers (1)

theIV
theIV

Reputation: 25774

Most rake tasks that are namespaced with "test" will only run on your test environment and not in other environments. It's hardcoded into the task as to mitigate potentially devastating affects they might have in an environment such as production.

You can see that these tasks don't take into account the environment in which they are called in the source.

If you want to run these tasks in whatever environment you want, your best bet is to recreate these tasks and pass in the environment.

namespace :any_environment_test do
  task :load => :environment do
    ...
  task :clone => :environment do
    ...

In this specific case, it's a little trickier, as it sounds like you want to clone from any environment to any environment. If this is the case, you should probably have two vars that are passed, such as FROM_ENV= and TO_ENV=.

Long story longer, you're going to write custom tasks, but can inspire yourself from the link I posted above. :)

Upvotes: 2

Related Questions