diff --git a/changelog/fix_false_positive_for_multiple_assertions_cop.md b/changelog/fix_false_positive_for_multiple_assertions_cop.md new file mode 100644 index 00000000..a63a7c6e --- /dev/null +++ b/changelog/fix_false_positive_for_multiple_assertions_cop.md @@ -0,0 +1 @@ +* [#223](https://github.com/rubocop/rubocop-minitest/issues/223): Fix a false positive for `Minitest/MultipleAssertions` when using assertion method with block. ([@fatkodima][]) diff --git a/lib/rubocop/cop/minitest/multiple_assertions.rb b/lib/rubocop/cop/minitest/multiple_assertions.rb index 27d044d6..c0224abd 100644 --- a/lib/rubocop/cop/minitest/multiple_assertions.rb +++ b/lib/rubocop/cop/minitest/multiple_assertions.rb @@ -50,8 +50,9 @@ def on_class(class_node) private def assertions_count(node) - base = assertion_method?(node) ? 1 : 0 - base + node.each_child_node.sum { |c| assertions_count(c) } + node.each_descendant(:send).count do |send_node| + assertion_method?(send_node) + end end def max_assertions diff --git a/test/rubocop/cop/minitest/multiple_assertions_test.rb b/test/rubocop/cop/minitest/multiple_assertions_test.rb index 41710023..337dbc0b 100644 --- a/test/rubocop/cop/minitest/multiple_assertions_test.rb +++ b/test/rubocop/cop/minitest/multiple_assertions_test.rb @@ -19,6 +19,20 @@ def test_asserts_twice RUBY end + def test_registers_offense_when_multiple_expectations_with_block + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_asserts_three_times + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [3/1]. + assert_equal(foo, bar) + assert_raises(SomeError) do + assert_equal(baz, bar) + end + end + end + RUBY + end + def test_checks_when_inheriting_some_class_and_class_name_ending_with_test assert_offense(<<~RUBY) class FooTest < ActiveSupport::TestCase