diff --git a/changelog/fix_error_multiple_assertions.md b/changelog/fix_error_multiple_assertions.md new file mode 100644 index 0000000..8f3c6bb --- /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 388ad60..0696eaf 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 b47d666..0509c12 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.'