Skip to content

Commit

Permalink
Add Rubocop Minitest (#885)
Browse files Browse the repository at this point in the history
* Add RuboCop minitest

* Fix safe offenses

- Minitest/AssertEmpty
- Minitest/AssertEmptyLiteral
- Minitest/AssertIncludes
- Minitest/AssertInstanceOf
- Minitest/AssertPredicate
- Minitest/EmptyLineBeforeAssertionMethods
- Minitest/LiteralAsActualArgument

* Fix unsafe offenses

- Minitest/AssertPredicate
- Minitest/AssertTruthy
- Minitest/RefuteFalse
- Minitest/RefuteIncludes
- Minitest/RefuteRespondTo

* Disable Minitest/MultipleAssertions cop

* Disable Minitest/NoAssertions in tests using mocha

Some tests are using Mocha `expects` and therefore do not require
`assert`.

This commit disables offense on those specific tests, leaving it enabled
for all other ones
  • Loading branch information
tagliala authored Oct 29, 2023
1 parent 3cc9e41 commit 7dc8cfe
Show file tree
Hide file tree
Showing 24 changed files with 432 additions and 62 deletions.
160 changes: 160 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---

require:
- rubocop-minitest
- rubocop-performance

AllCops:
Expand Down Expand Up @@ -46,6 +47,165 @@ Layout/TrailingWhitespace:
Layout/TrailingEmptyLines:
Enabled: true

Minitest:
Enabled: true

Minitest/AssertEmpty:
Enabled: true

Minitest/AssertEmptyLiteral:
Enabled: true

Minitest/AssertEqual:
Enabled: true

Minitest/AssertInDelta:
Enabled: true

Minitest/AssertIncludes:
Enabled: true

Minitest/AssertInstanceOf:
Enabled: true

Minitest/AssertKindOf:
Enabled: true

Minitest/AssertMatch:
Enabled: true

Minitest/AssertNil:
Enabled: true

Minitest/AssertOperator:
Enabled: true

Minitest/AssertOutput:
Enabled: true

Minitest/AssertPathExists:
Enabled: true

Minitest/AssertPredicate:
Enabled: true

Minitest/AssertRaisesCompoundBody:
Enabled: true

Minitest/AssertRaisesWithRegexpArgument:
Enabled: true

Minitest/AssertRespondTo:
Enabled: true

Minitest/AssertSame:
Enabled: true

Minitest/AssertSilent:
Enabled: true

Minitest/AssertTruthy:
Enabled: true

Minitest/AssertWithExpectedArgument:
Enabled: true

Minitest/AssertionInLifecycleHook:
Enabled: true

Minitest/DuplicateTestRun:
Enabled: true

Minitest/EmptyLineBeforeAssertionMethods:
Enabled: true

Minitest/GlobalExpectations:
Enabled: true

Minitest/LifecycleHooksOrder:
Enabled: true

Minitest/LiteralAsActualArgument:
Enabled: true

Minitest/MultipleAssertions:
Enabled: false

Minitest/NoAssertions:
Enabled: true

Minitest/NoTestCases:
Enabled: true

Minitest/NonPublicTestMethod:
Enabled: true

Minitest/RefuteEmpty:
Enabled: true

Minitest/RefuteEqual:
Enabled: true

Minitest/RefuteFalse:
Enabled: true

Minitest/RefuteInDelta:
Enabled: true

Minitest/RefuteIncludes:
Enabled: true

Minitest/RefuteInstanceOf:
Enabled: true

Minitest/RefuteKindOf:
Enabled: true

Minitest/RefuteMatch:
Enabled: true

Minitest/RefuteNil:
Enabled: true

Minitest/RefuteOperator:
Enabled: true

Minitest/RefutePathExists:
Enabled: true

Minitest/RefutePredicate:
Enabled: true

Minitest/RefuteRespondTo:
Enabled: true

Minitest/RefuteSame:
Enabled: true

Minitest/ReturnInTestMethod:
Enabled: true

Minitest/SkipEnsure:
Enabled: true

Minitest/SkipWithoutReason:
Enabled: true

Minitest/TestFileName:
Enabled: true

Minitest/TestMethodName:
Enabled: true

Minitest/UnreachableAssertion:
Enabled: true

Minitest/UnspecifiedException:
Enabled: true

Minitest/UselessAssertion:
Enabled: true

Performance:
Enabled: true

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ end

group :rubocop do
gem 'rubocop'
gem 'rubocop-minitest'
gem 'rubocop-performance'
end
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-minitest (0.33.0)
rubocop (>= 1.39, < 2.0)
rubocop-performance (1.19.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
Expand Down Expand Up @@ -272,6 +274,7 @@ DEPENDENCIES
rails (~> 7.1.0)
rails-controller-testing
rubocop
rubocop-minitest
rubocop-performance
simplecov
simplecov-cobertura
Expand Down
12 changes: 12 additions & 0 deletions test/aliases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,31 @@ def teardown
def test_assignments_before_calling_alias
Student.stubs(:new).returns(mock_student)
get :new

assert_response :success
assert_equal 'magical', assigns(:something)
end

def test_controller_should_render_new
Student.stubs(:new).returns(mock_student)
get :new

assert_response :success
assert_equal 'New HTML', @response.body.strip
end

def test_expose_the_requested_user_on_edit
Student.expects(:find).with('42').returns(mock_student)
get :edit, params: { id: '42' }

assert_equal mock_student, assigns(:student)
assert_response :success
end

def test_controller_should_render_edit
Student.stubs(:find).returns(mock_student)
get :edit, params: { id: '42' }

assert_response :success
assert_equal 'Edit HTML', @response.body.strip
end
Expand All @@ -83,6 +87,7 @@ def test_render_xml_when_it_is_given_as_a_block
@request.accept = 'application/xml'
Student.stubs(:find).returns(mock_student)
get :edit, params: { id: '42' }

assert_response :success
assert_equal 'Render XML', @response.body
end
Expand All @@ -91,6 +96,7 @@ def test_is_not_redirected_on_create_with_success_if_success_block_is_given
Student.stubs(:new).returns(mock_student(save: true))
@controller.stubs(:resource_url).returns('http://test.host/')
post :create

assert_response :success
assert_equal "I won't redirect!", @response.body
end
Expand All @@ -100,6 +106,7 @@ def test_dumb_responder_quietly_receives_everything_on_failure
Student.stubs(:new).returns(mock_student(save: false, errors: {some: :error}))
@controller.stubs(:resource_url).returns('http://test.host/')
post :create

assert_response :success
assert_equal "New HTML", @response.body.strip
end
Expand All @@ -109,13 +116,15 @@ def test_html_is_the_default_when_only_xml_is_overwriten
Student.stubs(:new).returns(mock_student(save: false, errors: {some: :error}))
@controller.stubs(:resource_url).returns('http://test.host/')
post :create

assert_response :success
assert_equal "New HTML", @response.body.strip
end

def test_wont_render_edit_template_on_update_with_failure_if_failure_block_is_given
Student.stubs(:find).returns(mock_student(update: false, errors: { fail: true }))
put :update, params: { id: '42' }

assert_response :success
assert_equal "I won't render!", @response.body
end
Expand All @@ -124,12 +133,14 @@ def test_dumb_responder_quietly_receives_everything_on_success
Student.stubs(:find).returns(mock_student(update: true))
@controller.stubs(:resource_url).returns('http://test.host/')
put :update, params: { id: '42', student: {these: 'params'} }

assert_equal mock_student, assigns(:student)
end

def test_block_is_called_when_student_is_destroyed
Student.stubs(:find).returns(mock_student(destroy: true))
delete :destroy, params: { id: '42' }

assert_response :success
assert_equal "Destroyed!", @response.body
end
Expand All @@ -140,6 +151,7 @@ def test_options_are_used_in_respond_with
Student.stubs(:new).returns(mock_student)

post :create

assert_equal "http://test.host/", @response.location
end

Expand Down
10 changes: 9 additions & 1 deletion test/association_chain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_begin_of_association_chain_is_called_on_index
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:all).returns(mock_pet)
get :index

assert_response :success
assert_equal 'Index HTML', @response.body.strip
end
Expand All @@ -54,6 +55,7 @@ def test_begin_of_association_chain_is_called_on_new
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:build).returns(mock_pet)
get :new

assert_response :success
assert_equal 'New HTML', @response.body.strip
end
Expand All @@ -62,6 +64,7 @@ def test_begin_of_association_chain_is_called_on_show
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:find).with('47').returns(mock_pet)
get :show, params: { id: '47' }

assert_response :success
assert_equal 'Show HTML', @response.body.strip
end
Expand All @@ -70,6 +73,7 @@ def test_instance_variable_should_not_be_set_if_already_defined
@controller.current_user.expects(:pets).never
Pet.expects(:find).never
get :edit, params: { id: '47' }

assert_response :success
assert_equal 'new pet', assigns(:pet)
end
Expand All @@ -78,13 +82,15 @@ def test_model_is_not_initialized_with_nil
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:build).with({}).returns(mock_pet)
get :new

assert_equal mock_pet, assigns(:pet)
end

def test_begin_of_association_chain_is_included_in_chain
@controller.current_user.expects(:pets).returns(Pet)
Pet.expects(:build).with({}).returns(mock_pet)
get :new

assert_equal [@controller.current_user], @controller.send(:association_chain)
end

Expand Down Expand Up @@ -120,14 +126,16 @@ def test_parent_is_added_to_association_chain
Puppet.expects(:find).with('42').returns(mock_puppet)
mock_puppet.expects(:destroy)
delete :destroy, params: { id: '42', pet_id: '37' }

assert_equal [mock_pet], @controller.send(:association_chain)
end

def test_parent_is_added_to_association_chain_if_not_available
Puppet.expects(:find).with('42').returns(mock_puppet)
mock_puppet.expects(:destroy)
delete :destroy, params: { id: '42' }
assert_equal [], @controller.send(:association_chain)

assert_empty @controller.send(:association_chain)
end

protected
Expand Down
Loading

0 comments on commit 7dc8cfe

Please sign in to comment.