diff --git a/lib/generators/rspec/scaffold/templates/api_controller_spec.rb b/lib/generators/rspec/scaffold/templates/api_controller_spec.rb index c8e519d5f..1a53e3e85 100644 --- a/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +++ b/lib/generators/rspec/scaffold/templates/api_controller_spec.rb @@ -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 @@ -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 diff --git a/lib/generators/rspec/scaffold/templates/api_request_spec.rb b/lib/generators/rspec/scaffold/templates/api_request_spec.rb index 065bbc98e..5b42a5146 100644 --- a/lib/generators/rspec/scaffold/templates/api_request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/api_request_spec.rb @@ -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 @@ -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 diff --git a/lib/generators/rspec/scaffold/templates/controller_spec.rb b/lib/generators/rspec/scaffold/templates/controller_spec.rb index 837e34e9b..f8b135dc2 100644 --- a/lib/generators/rspec/scaffold/templates/controller_spec.rb +++ b/lib/generators/rspec/scaffold/templates/controller_spec.rb @@ -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 @@ -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 diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index b93e75f36..2103e143b 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -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 @@ -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 diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 3c663dc83..d46f7e763 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -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 @@ -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+"/) diff --git a/spec/rspec/rails/matchers/have_http_status_spec.rb b/spec/rspec/rails/matchers/have_http_status_spec.rb index 82333c74c..dfb8a301b 100644 --- a/spec/rspec/rails/matchers/have_http_status_spec.rb +++ b/spec/rspec/rails/matchers/have_http_status_spec.rb @@ -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