spierepf
spierepf

Reputation: 2934

What is the correct way to run Rails tests without invoking the development environment?

I have an Rails app, with some initializer code which must be executed when the app is running in development mode. However, this initializer code must not be executed when running tests.

I have established that

$ rake test

causes the app to be run in development mode, which invokes the initializer code and therefore breaks my tests. This is expected behaviour apparently (see: https://github.com/rails/rails/issues/9801).

What is the correct command to run my Rails app tests without starting the app in development mode?

Upvotes: 0

Views: 83

Answers (3)

Fitter Man
Fitter Man

Reputation: 682

You need to have a line at the start of the test_helper.rb file that says

Rails.env = "test"

You can't use

ENV["RAILS_ENV"] = "test"

because it will fail to clear the cached value that returns from calls to Rails.env.

Upvotes: 0

Alex Ghiculescu
Alex Ghiculescu

Reputation: 7540

Does your test_helper.rb file look like the default? It should start with:

ENV["RAILS_ENV"] = "test"

Upvotes: 1

Raj Adroit
Raj Adroit

Reputation: 3878

Try running the rake task with an explicit environment:

rake test:units RAILS_ENV=test

If you don't specify an environment, development is assumed, in my experience. And while the test database still gets the fixture data inserted into it, stuff from the development environment still gets referenced for some reason.

Upvotes: 0

Related Questions