Skip to content

[WiP] Update of requests specs for invalid params for rails 6.1.1 and above #2466

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

Closed
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
13 changes: 13 additions & 0 deletions example_app_generator/generate_stuff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,17 @@ def using_source_path(path)
gsub_file 'spec/controllers/uploads_controller_spec.rb',
'skip("Add a hash of attributes valid for your model")',
'{}'

# adds validation to widget so our test for happy and unhappy path will be ready to test validation
gsub_file 'spec/requests/widgets_spec.rb',
'skip("Add a hash of attributes invalid for your model")',
'{ name: "" }'

gsub_file 'spec/requests/widgets_spec.rb',
'skip("Add a hash of attributes valid for your model")',
'{ name: "lorem" }'

gsub_file 'app/models/widget.rb',
'end',
'validates :name, presence: true; end'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pirj should I do it this way, or maybe I can just add validation to the widget model in the scaffold generator?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's quite good like this 👍

final_tasks
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 @@ -82,10 +82,17 @@
}.to change(<%= class_name %>, :count).by(0)
end

<% if Rails.version.to_f < 6.1 || Rails.version == '6.1.0' %>
it "renders a successful response (i.e. to display the 'new' template)" do
post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes }
expect(response).to be_successful
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting there.

  1) /widgets POST /create with invalid parameters renders a successful response (i.e. to display the 'new' template)
     Failure/Error: expect(response).to be_successful
       expected `#<ActionDispatch::TestResponse:0x0000564d388be7d8 @mon_mutex=#<Thread::Mutex:0x0000564d388be788>, @mo..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>>.successful?` to be truthy, got false
     # ./spec/requests/widgets_spec.rb:82:in `block (4 levels) in <top (required)>'

  2) /widgets PATCH /update with invalid parameters renders a successful response (i.e. to display the 'edit' template)
     Failure/Error: expect(response).to be_successful
       expected `#<ActionDispatch::TestResponse:0x0000564d38c3deb8 @mon_mutex=#<Thread::Mutex:0x0000564d38c3de68>, @mo..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>>.successful?` to be truthy, got false
     # ./spec/requests/widgets_spec.rb:114:in `block (4 levels) in <top (required)>'

I guess this should be

expect(response.status).to redirect_to(:new)

or something?

end
<% else %>
it "renders a response with 422 status - unporcessable entity" do
post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes }
expect(response.status).to eq(422)
end
<% end %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quite odd for a 6.1 build 🤔

   3) /widgets POST /create with invalid parameters renders a response with 422 status - unporcessable entity
     Failure/Error: expect(response.status).to eq(422)

       expected: 422
            got: 302

end
end

Expand All @@ -111,11 +118,19 @@
end

context "with invalid parameters" do
<% if Rails.version.to_f < 6.1 || Rails.version == '6.1.0' %>
it "renders a successful response (i.e. to display the 'edit' template)" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: invalid_attributes }
expect(response).to be_successful
end
<% else %>
it "renders a response with 422 status - unporcessable entity" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: invalid_attributes }
expect(response.status).to eq(422)
end
<% end %>
end
end

Expand Down