Skip to content

Commit 9d5fc66

Browse files
committed
Fix deprecation warning for :unprocessable_entity in scaffold-generated tests with Rack 3.2
For rack 3.1 and higher, this change uses `:unprocessable_content` instead of `:unprocessable_entity`. For rack 3.0 and below, it continues to use `:unprocessable_entity`.
1 parent b80f752 commit 9d5fc66

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

lib/generators/rspec/scaffold/templates/api_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
context "with invalid params" do
8080
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
8181
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
82-
expect(response).to have_http_status(:unprocessable_entity)
82+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
8383
expect(response.content_type).to eq('application/json')
8484
end
8585
end
@@ -110,7 +110,7 @@
110110
it "renders a JSON response with errors for the <%= singular_table_name %>" do
111111
<%= file_name %> = <%= class_name %>.create! valid_attributes
112112
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
113-
expect(response).to have_http_status(:unprocessable_entity)
113+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
114114
expect(response.content_type).to eq('application/json')
115115
end
116116
end

lib/generators/rspec/scaffold/templates/api_request_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
it "renders a JSON response with errors for the new <%= singular_table_name %>" do
8080
post <%= index_helper %>_url,
8181
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
82-
expect(response).to have_http_status(:unprocessable_entity)
82+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
8383
expect(response.content_type).to match(a_string_including("application/json"))
8484
end
8585
end
@@ -113,7 +113,7 @@
113113
<%= file_name %> = <%= class_name %>.create! valid_attributes
114114
patch <%= show_helper %>,
115115
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
116-
expect(response).to have_http_status(:unprocessable_entity)
116+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
117117
expect(response.content_type).to match(a_string_including("application/json"))
118118
end
119119
end

lib/generators/rspec/scaffold/templates/controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
context "with invalid params" do
9393
it "renders a response with 422 status (i.e. to display the 'new' template)" do
9494
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
95-
expect(response).to have_http_status(:unprocessable_entity)
95+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
9696
end
9797
end
9898
end
@@ -121,7 +121,7 @@
121121
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
122122
<%= file_name %> = <%= class_name %>.create! valid_attributes
123123
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
124-
expect(response).to have_http_status(:unprocessable_entity)
124+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
125125
end
126126
end
127127
end

lib/generators/rspec/scaffold/templates/request_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
8686
it "renders a response with 422 status (i.e. to display the 'new' template)" do
8787
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
88-
expect(response).to have_http_status(:unprocessable_entity)
88+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
8989
end
9090
end
9191
end
@@ -115,7 +115,7 @@
115115
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
116116
<%= file_name %> = <%= class_name %>.create! valid_attributes
117117
patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
118-
expect(response).to have_http_status(:unprocessable_entity)
118+
expect(response).to have_http_status(<%= Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422).inspect %>)
119119
end
120120
end
121121
end

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/)
3333
.and(contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/))
3434
)
35+
36+
expect(
37+
filename
38+
).to(
39+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
40+
contain(/expect\(response\).to have_http_status\(:unprocessable_entity\)/)
41+
else
42+
contain(/expect\(response\).to have_http_status\(:unprocessable_content\)/)
43+
end
44+
)
3545
end
3646
end
3747

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

112+
expect(
113+
filename
114+
).to(
115+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
116+
contain(/expect\(response\).to have_http_status\(:unprocessable_entity\)/)
117+
else
118+
contain(/expect\(response\).to have_http_status\(:unprocessable_content\)/)
119+
end
120+
)
121+
102122
expect(filename).not_to contain(/"renders a JSON response with the new \w+"/)
103123
expect(filename).not_to contain(/"renders a JSON response with errors for the new \w+"/)
104124
expect(filename).not_to contain(/"renders a JSON response with the \w+"/)

0 commit comments

Comments
 (0)