From 53d197d31de4796f5783a929cfcee3b76f6b89bd Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Fri, 23 Aug 2024 18:01:39 +0200 Subject: [PATCH] Fix an error for `Minitest/MultipleAssertions` when using for-style loops They produce an assign node, but the value is part of the parent for node --- changelog/fix_error_multiple_assertions.md | 1 + lib/rubocop/cop/minitest/multiple_assertions.rb | 6 +++++- .../cop/minitest/multiple_assertions_test.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_error_multiple_assertions.md diff --git a/changelog/fix_error_multiple_assertions.md b/changelog/fix_error_multiple_assertions.md new file mode 100644 index 00000000..8f3c6bbd --- /dev/null +++ b/changelog/fix_error_multiple_assertions.md @@ -0,0 +1 @@ +* [#317](https://github.com/rubocop/rubocop-minitest/pull/317): Fix an error for `Minitest/MultipleAssertions` when using for-style loops. ([@earlopain][]) diff --git a/lib/rubocop/cop/minitest/multiple_assertions.rb b/lib/rubocop/cop/minitest/multiple_assertions.rb index 388ad601..0696eafa 100644 --- a/lib/rubocop/cop/minitest/multiple_assertions.rb +++ b/lib/rubocop/cop/minitest/multiple_assertions.rb @@ -75,7 +75,11 @@ def assertions_count_based_on_type(node) end def assertions_count_in_assignment(node) - return assertions_count_based_on_type(node.expression) unless node.masgn_type? + unless node.masgn_type? + return 0 unless node.expression # for-style loop + + return assertions_count_based_on_type(node.expression) + end rhs = node.children.last diff --git a/test/rubocop/cop/minitest/multiple_assertions_test.rb b/test/rubocop/cop/minitest/multiple_assertions_test.rb index b47d6662..0509c12f 100644 --- a/test/rubocop/cop/minitest/multiple_assertions_test.rb +++ b/test/rubocop/cop/minitest/multiple_assertions_test.rb @@ -616,6 +616,20 @@ class FooTest < ActiveSupport::TestCase RUBY end + def test_registers_offense_when_for_style_loop + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_asserts_twice + ^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1]. + assert_equal(foo, bar) + for baz in [1, 2] + assert_equal(baz, 1) + end + end + end + RUBY + end + def test_registers_offense_when_complex_multiple_assignment_structure_and_multiple_assertions skip 'FIXME: The shared `@cop` instance variable causes flaky tests due to state changes.'