Skip to content
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

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

Closed
G-Rath opened this issue Dec 30, 2023 · 0 comments · Fixed by #290
Closed

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

G-Rath opened this issue Dec 30, 2023 · 0 comments · Fixed by #290

Comments

@G-Rath
Copy link
Contributor

G-Rath commented Dec 30, 2023

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
@koic koic closed this as completed in #290 Dec 31, 2023
koic added a commit that referenced this issue Dec 31, 2023
[Fix #289] don't count assertions twice when their return value is being assigned
koic added a commit that referenced this issue Jan 1, 2024
[Fix #289] count asserts within blocks for assignments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant