Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how can I eager load all code before a specific Rspec test?

I have some Rspec tests that are sometimes failing with an error: Circular dependency detected while autoloading constant. These tests are multithreaded (rufus-scheduler tests), which is apparently a known problem for autoloading code (http://route.github.io/2013/11/13/rails-autoloading.html).

I can get the tests to work consistently if I set config.eager_load = true in config/environments/test.rb. However, I'm worried that this will really slow down the rest of my test suite when it grows larger. Is there any way to set this eager load option ONLY for my multithreaded tests?

Rails 4.1.4

like image 965
Sterling Paramore Avatar asked Sep 11 '14 20:09

Sterling Paramore


People also ask

How do I run a specific test in RSpec?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

How do I run a test in Ruby on Rails?

2.7 The Rails Test Runner Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case. You can also run a particular test method from the test case by providing the -n or --name flag and the test's method name.


1 Answers

Add before { MyApp::Application.eager_load! } as the setup for this file/suite only. (Agreeing with @SterlingParamore above)

FYI: You need to put it in a before{} rather than just loose in the body of the describe, because otherwise it will occur whenever the file is loaded (e.g. even if you are loading that file, but targeting another spec with a tag, e.g. rspec spec --tag=focus).

Setting config.eager_load = true in your test.rb config will slow down time to first test, unless you are using Spring (which after all is designed to speed up time to first test on subsequent test runs). However, if you are using Spring, might be worth cross-referencing https://github.com/rails/spring/issues/519, though this relates to quite a different eager-loading problem.*

like image 161
Tim Diggins Avatar answered Oct 12 '22 00:10

Tim Diggins