Skip to content

Update of requests/controllers specs with invalid params for rails 7.0 #2585

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 3 commits into from
Mar 28, 2022
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
15 changes: 15 additions & 0 deletions lib/generators/rspec/scaffold/templates/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions lib/generators/rspec/scaffold/templates/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
20 changes: 18 additions & 2 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+"/) }

Expand Down