Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 269
John Bachir Avatar asked Mar 11 '15 17:03

John Bachir


People also ask

What is eager loading in Ruby on Rails?

Eager loading is a way to find objects of a certain class and a number of named associations. Here I share my thoughts on using it with Rails. What are N + 1 queries? It mainly occurs when you load the bunch of objects and then for each object you make one more query to find associated object.

What is config Eager_load?

eager_load when true, eager loads all registered config. eager_load_namespaces . This includes your application, engines, Rails frameworks and any other registered namespace. config. eager_load_namespaces registers namespaces that are eager loaded when config.


1 Answers

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.

like image 91
Mugur 'Bud' Chirica Avatar answered Nov 15 '22 06:11

Mugur 'Bud' Chirica