Skip to content

Scaffold-generated tests produce deprecation warning for :unprocessable_entity in Rack 3.2 #2859

@taketo1113

Description

@taketo1113

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.4.5
Rails version: 8.0.2
RSpec version: 3.13

  • rspec-core 3.13.5
  • rspec-expectations 3.13.5
  • rspec-mocks 3.13.5
  • rspec-rails 8.0.1
  • rspec-support 3.13.4

Observed behaviour

In rack v3.2, when running tests generated by rails generate scaffold / scaffold_controller, the following warning is displayed:

/path-to-app/vendor/bundle/ruby/3.4.0/gems/rspec-rails-8.0.1/lib/rspec/rails/matchers/have_http_status.rb:219: warning: Status code :unprocessable_entity is deprecated and will be removed in a future version of Rack. Please use :unprocessable_content instead.

For example, in the following request spec, the warning occurs at the expect(response).to have_http_status(:unprocessable_entity) line.

# spec/requests/posts_spec.rb
  describe "POST /create" do
...
    context "with invalid parameters" do
...
      it "renders a response with 422 status (i.e. to display the 'new' template)" do
        post posts_url, params: { post: invalid_attributes }
        expect(response).to have_http_status(:unprocessable_entity)
      end
    end
  end

Expected behaviour

When running tests generated by rails generate scaffold / scaffold_controller in rack v3.2, no warning should be displayed.

The reason for the warning is that starting in rack v3.2, passing obsolete values such as :unprocessable_entity to Rack::Utils.status_code will trigger a warning.
Replacing :unprocessable_entity with :unprocessable_content removes the warning, but it would be preferable if tests generated by rails generate scaffold / scaffold_controller did not produce warnings without manual changes.

Can you provide an example reproduction?

Example app

https://github.com/taketo1113/rails-sample-rspec-rails

Steps to reproduce

rails spec

The tests pass, but the following warning is displayed:

/path-to-app/vendor/bundle/ruby/3.4.0/gems/rspec-rails-8.0.1/lib/rspec/rails/matchers/have_http_status.rb:219: warning: Status code :unprocessable_entity is deprecated and will be removed in a future version of Rack. Please use :unprocessable_content instead.

Additional Information: workaround for existing code

After upgrading to rack 3.2, It can fix the warnings in existing test code by replacing :unprocessable_entity with :unprocessable_content in request specs and controller specs.

# spec/requests/posts_spec.rb
  describe "POST /create" do
...
    context "with invalid parameters" do
...
      it "renders a response with 422 status (i.e. to display the 'new' template)" do
        post posts_url, params: { post: invalid_attributes }
-        expect(response).to have_http_status(:unprocessable_entity)
+        expect(response).to have_http_status(:unprocessable_content)
      end
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions