diff --git a/lib/generators/rspec/scaffold/templates/controller_spec.rb b/lib/generators/rspec/scaffold/templates/controller_spec.rb index 879491e75..fe19a3518 100644 --- a/lib/generators/rspec/scaffold/templates/controller_spec.rb +++ b/lib/generators/rspec/scaffold/templates/controller_spec.rb @@ -90,10 +90,17 @@ end context "with invalid params" do + <% if Rails.version.to_f < 7.0 %> it "returns a success response (i.e. to display the 'new' template)" do post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session expect(response).to be_successful end + <% else %> + 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) + end + <% end %> end end @@ -118,11 +125,19 @@ end context "with invalid params" do + <% if Rails.version.to_f < 7.0 %> it "returns a success response (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 be_successful end + <% else %> + 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) + end + <% end %> end end diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index a3a105389..6d6fdab9e 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -83,10 +83,17 @@ }.to change(<%= class_name %>, :count).by(0) end + <% if Rails.version.to_f < 7.0 %> it "renders a successful response (i.e. to display the 'new' template)" do post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes } expect(response).to be_successful end + <% else %> + 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) + end + <% end %> end end @@ -112,11 +119,19 @@ end context "with invalid parameters" do + <% if Rails.version.to_f < 7.0 %> it "renders a successful response (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 be_successful end + <% else %> + 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) + end + <% end %> end end diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 4638052b4..21c63a63a 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -20,6 +20,14 @@ it { is_expected.to contain('get post_url(post)') } it { is_expected.to contain('redirect_to(post_url(Post.last))') } it { is_expected.to contain(/"redirects to the \w+ list"/) } + + if ::Rails::VERSION::STRING >= '7.0.0' + it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) } + it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/) } + else + it { is_expected.to contain(/renders a successful response \(i.e. to display the 'new' template\)/) } + it { is_expected.to contain(/renders a successful response \(i.e. to display the 'edit' template\)/) } + end end describe 'with --no-request_specs' do @@ -62,13 +70,21 @@ it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) } it { is_expected.to contain(/GET #new/) } it { is_expected.to contain(/"redirects to the created \w+"/) } - it { is_expected.to contain(/display the 'new' template/) } + if ::Rails::VERSION::STRING >= '7.0.0' + it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) } + else + it { is_expected.to contain(/returns a success response \(i.e. to display the 'new' template\)/) } + end it { is_expected.not_to contain(/"renders a JSON response with the new \w+"/) } it { is_expected.not_to contain(/"renders a JSON response with errors for the new \w+"/) } it { is_expected.to contain(/GET #edit/) } it { is_expected.to contain(/"redirects to the \w+"/) } - it { is_expected.to contain(/display the 'edit' template/) } + if ::Rails::VERSION::STRING >= '7.0.0' + it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/) } + else + it { is_expected.to contain(/returns a success response \(i.e. to display the 'edit' template\)/) } + end it { is_expected.not_to contain(/"renders a JSON response with the \w+"/) } it { is_expected.not_to contain(/"renders a JSON response with errors for the \w+"/) }