Error when running RSpec tests: "FrozenError: can't modify frozen Array"

125 views Asked by At

I encountered an issue while running RSpec tests in my Rails application, and I'm seeking help in understanding and resolving it.

When attempting to run RSpec tests, specifically the tests located in the spec/requests directory, I encountered the following error:

Failure/Error: require_relative '../config/environment'

FrozenError:
  can't modify frozen Array: ["/usr/local/lib/ruby/gems/3.2.0/gems/hotwire-livereload-1.3.1/app/channels", "/usr/local/lib/ruby/gems/3.2.0/gems/hotwire-livereload-1.3.1/app/helpers", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/channels", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/controllers", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/controllers/concerns", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/helpers", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/models", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/models/concerns", "/usr/local/lib/ruby/gems/3.2.0/gems/turbo-rails-1.5.0/app/jobs", "/usr/local/lib/ruby/gems/3.2.0/gems/importmap-rails-2.0.1/app/helpers", "/usr/local/lib/ruby/gems/3.2.0/gems/actiontext-7.1.3/app/helpers", "/usr/local/lib/ruby/gems/3.2.0/gems/actiontext-7.1.3/app/models"]
# ./config/environment.rb:5:in `<top (required)>'
# ./spec/rails_helper.rb:17:in `require_relative'
# ./spec/rails_helper.rb:17:in `<top (required)>'
# ./spec/requests/teams_spec.rb:1:in `<top (required)>'
No examples found.

Rails_helper.rb

require 'simplecov'

SimpleCov.start do
  add_filter "/helpers"
  add_filter "/config"
  add_filter "/spec"

  add_group "Controllers", "app/controllers"
  add_group "Models", "app/models"
end

require 'spec_helper'

ENV['RAILS_ENV'] ||= 'test'

require_relative '../config/environment'

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'rspec/rails'
require 'support/factory_bot'

# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  abort(e.to_s.strip)
end

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{Rails.root}/spec/fixtures"

  # Devise Testing
  config.include(Devise::Test::IntegrationHelpers, type: :request)

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # You can uncomment this line to turn off ActiveRecord support entirely.
  # config.use_active_record = false

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, type: :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://rspec.info/features/6-0/rspec-rails
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")

  # Faker configs
  Faker::Config.locale = :en
end

Environment.rb

require_relative "application"

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

Update: I have updated my question with more code, any other suggestions would be appericated.

0

There are 0 answers