RuntimeError: can't modify frozen Array (Rollbar, Rails 5.1 upgrade)

大憨熊 提交于 2020-01-05 05:31:14

问题


Unable to use rspec and rollbar after upgrading to rails 5.

  1. Create a Rails 4 app
  2. Upgrade gemfile to use rails 5
  3. Try adding rollbar gem/support

Standard config/environment.rb:

 # Load the Rails application.
require_relative 'application'

# Initialize the Rails application.
Rails.application.initialize!

Error when running rspec:

An error occurred while loading {path to specific spec file}
Failure/Error: require File.expand_path('../../config/environment', __FILE__)

RuntimeError:
  can't modify frozen Array
# ./config/environment.rb:6:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
...
No examples found.

回答1:


In most cases, that error is a red herring for something else.

When encountering it, don't get overwhelmed with the recurrent can't modify frozen Array error messages, and instead check the very first error that appears when running a spec.

For example:

Failure/Error: validate :uniqueness, if: 'should_be_unique?'

ArgumentError: Passing string to be evaluated in :if and :unless conditional options is not supported. Pass a symbol for an instance method, or a lambda, proc or block, instead.




回答2:


Debugging this is not easy but the solution is simple: Add a Module ("namespace" of your choice) around your app class definition in config/application.rb.

The module won't affect much. The only difference I could find is when printing out your app it will now appear as (that's how we found the fix vs a new working app):

<MyTestAPP::Application ...> instead of <Application ...>

Change:

class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
  end

To:

Module MyTestApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
  end
end


来源:https://stackoverflow.com/questions/49680075/runtimeerror-cant-modify-frozen-array-rollbar-rails-5-1-upgrade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!