Search code examples
ruby-on-railsrubyrake

Why is Rails running in development mode during rake test task?


Why is the following Rake task being run in Rails' development environment?

namespace :app_tests do
  desc "Run unit tests."
  task unit: [:environment] do
    RAILS_ENV = "test"
    Rails.env = "test"
    system "rake test RAILS_ENV=test"
  end
end

I was running into require errors caused by missing (test group) dependencies and it turns out that's because app_tests:unit is being run in Rails' development environment when run using bundle exec rake app_tests:unit.

How can I force this task to run in Rails' test environment?


Solution

  • Development environmnet is run by default, you need to declare the testing environment explicitly RAILS_ENV=test rake app_tests:unit

    You can enforce the env var in your task specifically by doing something like what they did with rspec/core here