I recently upgraded to Rails 5.1 from v4.3 and am now getting this error when running tests:
An error occurred while loading
./spec/controllers/admin/capac
I'll post my cause, but first I'll post a troubleshooting path to help you find your cause, as this error can show up for a myriad of reasons.
This is a fairly useless error in most contexts, and the stack trace is impenetrable. I've exampined all the gem code in the stack, and none of it points to the actual source of the problem:
# /Users/dh/.gem/gems/actionpack-5.2.3/lib/action_dispatch/middleware/stack.rb:76:in `insert'
# /Users/dh/.gem/gems/actionpack-5.2.3/lib/action_dispatch/middleware/stack.rb:76:in `insert'
# /Users/dh/.gem/gems/actionpack-5.2.3/lib/action_dispatch/middleware/stack.rb:83:in `insert_after'
# /Users/dh/.gem/gems/request_store-1.4.1/lib/request_store/railtie.rb:5:in `block in <class:Railtie>'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers'
# /Users/dh/.gem/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
To track down why you're getting this error, first comment out (or change back) whatever you did last and see if the problem goes away. If not, restore that line of code, then git stash
to hide all your recent changes and run tests.
That will probably fix the issue, and you can git stash pop
and start dividing your changes in half until you identify where the problem is.
If git stash
does not fix the issue, then you know the problem is either external to your code, or was present in your last commit.
If you know your previous commit tested clean, start looking at your databases, bundle your gems, changes to ENV vars you use, or other environmental factors your app depends on.
If git stash
did not fix the problem, and you don't know if your last commit was previously testing clean, start digging back through your commits, either manually or with git bisect
. If you can find a commit that started breaking tests, you'll be able to track down the line that is triggering this error.
--
As for my particular issue, it was a typo in a new has_many
relationship I added:
has_many :marijunas, class_name: Drug
Once I narrowed down the change to that line, the fix became obvious:
has_many :marijunas, class_name: :Drug
Try setting config.eager_load = true
in your environment file. This will load all files in memory during boot & will point out existing syntax errors in first place if any.