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

前端 未结 14 2026
面向向阳花
面向向阳花 2021-01-03 20:10

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         


        
相关标签:
14条回答
  • 2021-01-03 20:58

    My two-cents, as it may help someone else. I had this same issue when upgrading rails 4.2 to 5.0 I saw a thread post somewhere that suggested running one spec-test by itself / at a time. When I did that, I got a different error: superclass must be a Class and pointed me to my new .../models/application_record.rb file, in which I had made a syntax error by omission:

    class ApplicationRecord < ActiveRecord                              
      self.abstract_class = true
    end
    

    The above is missing ::Base after ActiveRecord -- first line.

    class ApplicationRecord < ActiveRecord::Base                                
      self.abstract_class = true
    end
    

    Adding that back in fixed all my specs.

    0 讨论(0)
  • 2021-01-03 21:00

    In case this helps anyone in future, it was database connection issue (Postgresql in my case). Head over to config/database.yml file and supply username: and password: < default is toor>

    Add this in the development: group.

    0 讨论(0)
  • 2021-01-03 21:01

    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!

    0 讨论(0)
  • 2021-01-03 21:01

    I tried to update from Rails 3 to Rails 5. I just set config.eager_load option and it helped. Either to false or true. Not nil, please

    0 讨论(0)
  • 2021-01-03 21:01

    In my case it was due to a git conflict message (the one like >>>> ... === ... <<<<) that I missed to resolve, so there was a syntax error when Rspec has been trying to load that class.

    --fail-fast command line option does not help in this case as the error happens higher in the call stack, but you can see the actual error by just running any single spec file.

    0 讨论(0)
  • 2021-01-03 21:02

    In my case recent rails_admin required some addition

    RuntimeError: Required middlewares for RailsAdmin are not added
    To fix tihs, add

    config.middleware.use ActionDispatch::Flash
    

    to config/application.rb.

    Then all rspecs became green again.

    0 讨论(0)
提交回复
热议问题