Skip to content

Fix deprecation warning for :unprocessable_entity in scaffold-generated tests with Rack 3.2 #2860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
context "with invalid params" do
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
expect(response.content_type).to eq('application/json')
end
end
Expand Down Expand Up @@ -110,7 +110,7 @@
it "renders a JSON response with errors for the <%= singular_table_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
expect(response.content_type).to eq('application/json')
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/rspec/scaffold/templates/api_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
post <%= index_helper %>_url,
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
Expand Down Expand Up @@ -113,7 +113,7 @@
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>,
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/rspec/scaffold/templates/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
context "with invalid params" do
it "renders a response with 422 status (i.e. to display the 'new' template)" do
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
end
end
end
Expand Down Expand Up @@ -121,7 +121,7 @@
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/rspec/scaffold/templates/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
it "renders a response with 422 status (i.e. to display the 'new' template)" do
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
end
end
end
Expand Down Expand Up @@ -115,7 +115,7 @@
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/)
.and(contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/))
)

expect(
filename
).to(
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
contain(/expect\(response\).to have_http_status\(:unprocessable_entity\)/)
else
contain(/expect\(response\).to have_http_status\(:unprocessable_content\)/)
end
)
end
end

Expand Down Expand Up @@ -99,6 +109,16 @@
expect(filename).to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/)
.and(contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/))

expect(
filename
).to(
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
contain(/expect\(response\).to have_http_status\(:unprocessable_entity\)/)
else
contain(/expect\(response\).to have_http_status\(:unprocessable_content\)/)
end
)

expect(filename).not_to contain(/"renders a JSON response with the new \w+"/)
expect(filename).not_to contain(/"renders a JSON response with errors for the new \w+"/)
expect(filename).not_to contain(/"renders a JSON response with the \w+"/)
Expand Down
1 change: 1 addition & 0 deletions spec/rspec/rails/matchers/have_http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def create_response(opts = {})

context 'with deprecated rack status codes' do
it 'supports the original names' do
allow(Rack::Utils).to receive(:warn).with(/unprocessable_entity is deprecated/, anything)
expect(create_response(status: 422)).to have_http_status(:unprocessable_entity)
end
end
Expand Down