diff --git a/changelog/fix_multiple_assertions_false_positive_methods_with_receiver.md b/changelog/fix_multiple_assertions_false_positive_methods_with_receiver.md new file mode 100644 index 00000000..ea1f85f6 --- /dev/null +++ b/changelog/fix_multiple_assertions_false_positive_methods_with_receiver.md @@ -0,0 +1 @@ +* [#321](https://github.com/rubocop/rubocop-minitest/issues/321): Fix a false positive for `Minitest/MultipleAssertions` for assert_/refute_ methods with a receiver. ([@MatzFan][]) diff --git a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb index ffa39ce2..4bb7382c 100644 --- a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +++ b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb @@ -99,7 +99,7 @@ def assertions_count(node) end end - # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize def assertion_method?(node) return false unless node return assertion_method?(node.expression) if node.assignment? && node.respond_to?(:expression) @@ -108,10 +108,10 @@ def assertion_method?(node) ASSERTION_PREFIXES.any? do |prefix| method_name = node.method_name - method_name.start_with?(prefix) || node.method?(:flunk) + (method_name.start_with?(prefix) && !node.receiver) || node.method?(:flunk) end end - # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity + # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize def lifecycle_hook_method?(node) node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name) diff --git a/test/rubocop/cop/minitest/multiple_assertions_test.rb b/test/rubocop/cop/minitest/multiple_assertions_test.rb index 0509c12f..3a1ea2b4 100644 --- a/test/rubocop/cop/minitest/multiple_assertions_test.rb +++ b/test/rubocop/cop/minitest/multiple_assertions_test.rb @@ -7,6 +7,17 @@ def setup configure_max_assertions(1) end + def test_checks_only_assertion_methods_with_no_receiver + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + # assert_something has a receiver + def test_asserts_once + assert_equal(foo, Bar.assert_something) + end + end + RUBY + end + def test_registers_offense_when_multiple_expectations assert_offense(<<~RUBY) class FooTest < Minitest::Test