rubocop / rubocop-minitest

Code style checking for Minitest files.
https://docs.rubocop.org/rubocop-minitest
MIT License
144 stars 44 forks source link

Minitest/MultipleAssertions counts assignments of assertion method returns twice #289

Closed G-Rath closed 11 months ago

G-Rath commented 11 months ago

The assertion counter defaults to checking the children of nodes and the node itself using assertion_method? - this results in double counting when the node is an assignment of the result of an assertion method because assertion_method? itself is "assignment aware":

send:'assert_equal(1, 2)' is an assertion method!
lvasgn:'_ = assert_equal(1, 2)' is an assertion method!

Expected behavior

The test to pass

Actual behavior

The test fails:

  2) Failure:
MultipleAssertionsTest#test_assignments_are_not_counted_twice [/home/jones/workspace/projects-oss/rubocop-minitest/test/rubocop/cop/minitest/multiple_assertions_test.rb:25]:
--- expected
+++ actual
@@ -1,5 +1,6 @@
 "class FooTest < Minitest::Test
   def test_asserts_once
+  ^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
     _ = assert_equal(1, 2)
   end
 end

Steps to reproduce the problem

Add the following test:

  def test_assignments_are_not_counted_twice
    assert_no_offenses(<<~RUBY)
      class FooTest < Minitest::Test
        def test_asserts_once
          _ = assert_equal(1, 2)
        end
      end
    RUBY
  end

RuboCop version

$ [bundle exec] rubocop -V
1.59.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.2.2) [x86_64-linux]
  - rubocop-minitest 0.34.2