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/ruby-2.3.1#Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:74:in `insert'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/actionpack-5.1.0/lib/action_dispatch/middleware/stack.rb:81:in `insert_after'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/request_store-1.3.2/lib/request_store/railtie.rb:5:in `block in <class:Railtie>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `instance_exec'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:30:in `run'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:59:in `block in run_initializers'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/initializable.rb:58:in `run_initializers'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/railties-5.1.0/lib/rails/application.rb:353:in `initialize!'
# ./config/environment.rb:5:in `<top (required)>'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_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/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:292:in `block in require'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_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/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `block in load'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:258:in `load_dependency'
# /Users/jeremynagel/.rvm/gems/ruby-2.3.1#Rails4.2_EnergyLink/gems/activesupport-5.1.0/lib/active_support/dependencies.rb:286:in `load'
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.
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.
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!
Check that your Test DB is up to date before anything else.
rake db:test:prepare.
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
I did rspec | less to find the first error
When i got the same error with similar stacktrace, it helped me to fix a syntax error in factories.
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.
Suggestion: try loading the app in development after adding config.eager_load = true to your development.rb file, which should give you an actually useful error message pinpointing the problem code.
In my case, a Rails 5.2 app, it was purely a result of upgrading from Ruby 2.4.5 to 2.5.8 which pinpointed the sole issue was unmatched range specifier in char-class in a very long regex that has been working for years.
I think when i had similar problem, it was due to syntax error. Try to run rails db:migrate to show more information on the line where error occur
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.
It was because I forgot to add bullet to the test group in Gemfile, looking at the top of the errors I saw:
NameError:
uninitialized constant Bullet
I had this same issue when working on an application with Rails 6.
The issue was that when I ran the command rspec I get the error below. The output below is shortened:
An error occurred while loading ./spec/routing/applyportal/school_histories_routing_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)
FrozenError:
can't modify frozen Array: ["/home/promisechukwuenyem/Projects/myapp/app/channels",
Here's how I solved it:
The issue was a result of errors in my code after I did a git pull.
Since I was running in development, I tried dropping my database using:
rails db:drop
Note: This did not drop my database, as the command was interrupted by the error in my code.
And then the actual place where the error was showed up in my terminal which was my config/route.rb file.
I had open my config/route.rb file. And then I found this:
# frozen_string_literal: true
Rails.application.routes.draw do
<<<<<<< HEAD
resources :courses
=======
resources :results
>>>>>>> Added result and result upload feature
resources :books
resources :schools
end
And then modified it to this:
# frozen_string_literal: true
Rails.application.routes.draw do
resources :books
resources :results
resources :schools
end
That's all.
I hope this helps
In my case, it turned out to be an issue with a config.after_initialize block failure, hidden during a rspec run.
However, the error surfaced when I called $ rails c -e test. Hope this helps someone with this being my first post.
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
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.
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.
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.
I got this error when I removed the gem 'Faker' but left the require 'faker' inside factories.rb
Try to run redis and sidekiq on background. In my case it was one of reasons.
So it's been a while since I fired up the old Rails server. It took a lot of tricks to even get the bundle to install... but eventually I upgraded from Rails 4.2.6 to 4.2.9. The bundle installs just fine. I'm on Ruby 2.3.0. But now the problem is... whenever I try to run:
rails s
I get the following error:
rails-html-sanitizer.rb:2:in `require': cannot load such file -- loofah (LoadError)
from /home/rainless/.rvm/gems/ruby-2.3.0#rails3.2.16/gems/rails-html-sanitizer-1.0.3/lib/rails-html-sanitizer.rb:2:in `<top (required)>'
from /home/rainless/.rvm/gems/ruby-2.3.0#global/gems/bundler-1.7.9/lib/bundler/runtime.rb:76:in `require'
from /home/rainless/.rvm/gems/ruby-2.3.0#global/gems/bundler-1.7.9/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
from /home/rainless/.rvm/gems/ruby-2.3.0#global/gems/bundler-1.7.9/lib/bundler/runtime.rb:72:in `each'
Or some variation of that.
At first I was getting around this by uninstalling whatever the Gem that was said to be unable to load (in this case "loofah" but I've done it with many more), and then adding it to the Gemfile and installing it using Bundler. I now realize that the list is never-ending:
gem 'rails-html-sanitizer'
gem 'chronic'
gem 'multipart-post'
gem 'oauth-active-resource'
gem 'rack-test'
gem 'multi_json'
gem 'addressable'
gem 'orm_adapter'
All the above couldn't load. I ran Rails for years before this... and I know this isn't the way it's supposed to work. Any help/advice on what could be going wrong?
If you see carefully in the error message, it says ruby-2.3.0#rails3.2.16 but you mentioned "upgraded from Rails 4.2.6 to 4.2.9." . For some reason, it's still referring to rails3.2.16. Therefore it could be you need to install the rails-html-sanitizer globally. By the way, Loofah is only needed if you wanted to use it in a non-rails app. Refer to the docs here.
Rails Html Sanitizer is only intended to be used with Rails
applications. If you need similar functionality in non Rails apps
consider using Loofah directly (that's what handles sanitization under
the hood).
from /home/rainless/.rvm/gems/ruby-2.3.0#rails3.2.16/gems/rails-html-sanitizer-1.0.3/lib/rails-html-sanitizer.rb:2:in `<top (required)>'
I am interested in playing around with the various stackdriver offerings, but I can't seem to get it to work with Rails. I added stackdriver to my Gemfile, ran bundle install and have added the config blocks.
Here's the error I am getting:
/usr/local/opt/rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/railties-4.2.7.1/lib/rails/railtie/configuration.rb:95:in `method_missing': undefined method `google_cloud' for #<Rails::Application::Configuration:0x007fbc843e0c18> (NoMethodError)
from /Users/preston/projects/myapp/config/environments/development.rb:74:in `block in <top (required)>'
I have tried requiring the Gem manually, among other things, and it doesn't seem like the code is getting loaded.
It turned out to be an issue that was causing an old version of the stackdriver gem (0.2.2) to be used. This happened due to a conflict with a different Google gem.
I have just started learning to use RSpec on my rails application.
It all seems to work ok, but when I run rspec spec I get pages and pages of what I think are lint messages.
I don't mind the ones that refer to my code, but lots of them refer to Gems that I am using.
I can't really fix those.
How can I configure RSpec to apply lint only to my code and not external Gems?
Here is a small sample. I get over 2000 lines of this stuff.
/Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/sorcery-0.8.5/lib/sorcery/model.rb:265: warning: method redefined; discarding old username_attribute_names=
/Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/activesupport-3.2.18/lib/active_support/dependencies.rb:251: warning: loading in progress, circular require considered harmful - /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/sorcery-0.8.5/lib/sorcery.rb
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/rspec:23:in `<main>'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/bin/rspec:23:in `load'
from /Users/jcreasey/.rvm/gems/ruby-2.0.0-p0/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
from /Users/jcreasey
This is my .rspec file
-color
--warnings
--require spec_helper
If you have /.rspec file remove --warning option from it.
Ruby 1.8.6, Rails 2.2.2, OS X Tiger
My Test::Unit tests started returning the error below. The relevant line seems to be:
`load_missing_constant':
Expected /Users/ethan/project/mtc/webcalendars/app/models/calendar.rb
to define Calendar (LoadError)
The file mentioned, calendar.rb looks fine. I can't find any errors in it. I tried removing the unit and functional test files for the Calendar model, but that had no effect on the error.
In the browser the application seems to be functioning normally.
Any insights? Can anyone suggest a way to isolate the problem?
Longer excerpt:
$ rake test
(in /Users/ethan/project/mtc/webcalendars)
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb"
"test/unit/calendar_event_test.rb" "test/unit/calendar_test.rb" "test/unit/committee_test.rb"
"test/unit/event_test.rb" "test/unit/general_app_mailer_test.rb" "test/unit/location_test.rb"
"test/unit/persistent_login_test.rb" "test/unit/role_test.rb" "test/unit/user_role_test.rb"
"test/unit/user_test.rb"
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:428:in
`load_missing_constant': Expected /Users/ethan/project/mtc/webcalendars/app/models/calendar.rb to define Calendar (LoadError)
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:77:in `const_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:89:in `const_missing'
[ ... ]
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib
/rake/rake_test_loader.rb"
Errors running test:units and test:functionals!
Ethan,
The only time I've seen this is when I've defined multiple classes in one file. I don't use test:unit too much myself but is it possible that you've defined another class before defining Calendar?
Hope that helps.