FrozenError with rack-test

I’m trying to convert the RSpec setup to Minitest, but hit a wall when trying to use the app via rack-test.

From what I can see, the request tests generated by hanami-rspec boil down to this on a hanami console e.g. with my minimal demo app from another issue:

require "rack/test"
include Rack::Test::Methods
def app = Hanami.app
get "/"

This raises:

/Users/myself/.gem/ruby/3.3.0/gems/rack-test-2.1.0/lib/rack/test/methods.rb:32:in `rack_test_session': 
can't modify frozen Hanami::Config::Logger: 
#<Hanami::Config::Logger:0x00000001209f0800 @__config__=#<Dry::Configurable::Config values={:level=>:debug, :logger_constructor=>#<Method: Hanami::Config::Logger#development_logger(_env, app_name, **options) /Users/myself/.gem/ruby/3.3.0/gems/hanami-2.2.1/lib/hanami/config/logger.rb:153>, :stream=>#<IO:<STDOUT>>, :formatter=>:string, :template=>:details, :filters=>["_csrf", "password", "password_confirmation"], :options=>{}}>, @app_name=#<Hanami::SliceName:0x00000001209d9448 @slice=Demo::App, @inflector=#<Method: #<Class:Demo::App>(Hanami::Slice::ClassMethods)#inflector() /Users/myself/.gem/ruby/3.3.0/gems/hanami-2.2.1/lib/hanami/slice.rb:237>>, @env=:development> (FrozenError)

I have to admit that I haven’t used RSpec in a long while and it has evolved quite a lot ever since. My poking around in the backtrace has not led to much, apparently, there’s something different happening when RSpec is running a request test than what meets the my eye. Any idea what it could be?

Figured it out: This happens if Rack::Test::Methods is included into the wrong context. In Minitest, the describe block (in this not refactored example) is where things have to go:

# frozen_string_literal: true

require_relative '../spec_helper'
# include Rack::Test::Methods       # 💣

describe "Root" do
  include Rack::Test::Methods       # 🎉

  def app
    Hanami.app
  end

  it "is found" do
    get "/"
    _(last_response.status).must_equal 200
  end
end
1 Like