Skip to content

Commit

Permalink
Fix an error for Minitest/MultipleAssertions when using for-style l…
Browse files Browse the repository at this point in the history
…oops

They produce an assign node, but the value is part of the parent for node
  • Loading branch information
Earlopain committed Aug 23, 2024
1 parent 859751a commit 53d197d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_error_multiple_assertions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#317](https://github.com/rubocop/rubocop-minitest/pull/317): Fix an error for `Minitest/MultipleAssertions` when using for-style loops. ([@earlopain][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/minitest/multiple_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions test/rubocop/cop/minitest/multiple_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.'

Expand Down

0 comments on commit 53d197d

Please sign in to comment.