John Bachir
John Bachir

Reputation: 22711

Should eager loading be on or off in the test environment?

This is in the standard test.rb:

# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false

Does this advice apply to when running the entire test suite, or only when running a single test?

How do I decide whether to turn this on or off?

Is there a nifty recipe for having it false when running individual tests and true when running the whole suite?

(I am suspicious that some types of errors I am getting are the result of this being off)

Upvotes: 6

Views: 1667

Answers (1)

Theo Chirica
Theo Chirica

Reputation: 4516

Eager loading applies for each test, so by extension even when you run the whole test suite.

Usually the tools referred in the comments are testing tools (like capybara), and you are pretty much required to have the eager_loading to true otherwise you'll get missing constants errors.

If you have a need to run some tests with eager_loading set to false and others with true you can use something like this:

# config/environments/test.rb

  config.eager_load = !!(ENV['ENABLE_SPRING'] == 'true')

and you run that particular batch of tests like so

$ ENABLE_SPRING=true bundle exec rspec spec/test_name.rb

Source: personal experience + https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-in-the-test-environment.

Upvotes: 1

Related Questions