Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails : RuntimeError - can't modify frozen Array when running rspec in rails

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/capacity_charges_controller_spec.rb.
Failure/Error: require File.expand_path('../../config/environment', __FILE__)

RuntimeError:
  can't modify frozen Array

I get it for every test file. The line that triggers the error comes from rails_helper. I've checked rails 5.1 sample repos and there's nothing substantially different about our version.

The full stack trace is:

RuntimeError:


can't modify frozen Array
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:81:in `insert_after'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/request_store-1.3.2/lib/request_store/railtie.rb:5:in `block in <class:Railtie>'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `instance_exec'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `run'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:59:in `block in run_initializers'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:58:in `run_initializers'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/railties-5.1.0/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# ./spec/rails_helper.rb:12:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# ./spec/controllers/admin/capacity_charges_controller_spec.rb:3:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `block in load'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/[email protected]_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
like image 708
jeznag Avatar asked Nov 16 '17 02:11

jeznag


4 Answers

Turns out this was caused by test failures higher in the stack trace. It was hard to see what the root cause was because there was so much in the console. If you get this issue, I'd suggest using --fail-fast so you don't get overwhelmed with recurrent error messages. Then check what the first error is.

like image 59
jeznag Avatar answered Oct 23 '22 11:10

jeznag


For me it was a FactoryBot update from version 4 to 5. I didn't see the deprecation warning because I jumped versions:

DEPRECATION WARNING: Static attributes will be removed in FactoryBot 5.0.
like image 25
schmijos Avatar answered Oct 23 '22 10:10

schmijos


Sometimes this exception is also raised if you have a wrong class name inheritance in your Ruby classes. Remember that controllers, models, etc, are all constants in Ruby.

This is an example off the top-of-my-head of a situation I encountered. Actual syntax may be off, but this was the concept that caused my problem.

module Admin
  class BaseController < ::ApplicationController
    ...
  end
end

# throws error, referencing constant that does not exist.
# Should be < ::Admin::BaseController
class OtherController < ::BaseController
end

Like others, my entire rspec suite would fail with the error can't modify a frozen Array, which isn't a very helpful stacktrace in this instance, per se.

Hope this save someone else from spending hours hunting down everything else!

like image 4
Dan L Avatar answered Oct 23 '22 12:10

Dan L


Check that your Test DB is up to date before anything else.

rake db:test:prepare.

like image 3
monkeyshoulders Avatar answered Oct 23 '22 12:10

monkeyshoulders