Skip to content

Commit 0dd2bce

Browse files
authored
Merge pull request #340 from Earlopain/multiple-assertions-receiver
[Fix #321] Fix false positives for `Minitest/MultipleAssertions` when the assertion has a receiver
2 parents c47ff6b + a700aa2 commit 0dd2bce

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#321](https://github.com/rubocop/rubocop-minitest/issues/321): Fix false positives for `Minitest/MultipleAssertions` when the assertion has a receiver. ([@earlopain][])

lib/rubocop/cop/mixin/minitest_exploration_helpers.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,24 @@ def assertions_count(node)
114114
end
115115
end
116116

117-
# rubocop:disable Metrics/CyclomaticComplexity
118117
def assertion_method?(node)
119118
return false unless node
120119
return assertion_method?(node.expression) if node.assignment? && node.respond_to?(:expression)
121120
return false unless node.type?(:send, :any_block)
122121

123-
method_name = node.method_name
124-
assertion_method = ASSERTION_PREFIXES.any? { |prefix| method_name.start_with?(prefix.to_s) }
125-
126-
assertion_method || node.method?(:flunk) || MATCHER_METHODS.include?(method_name)
122+
assertion_prefix_method?(node) || node.method?(:flunk) || MATCHER_METHODS.include?(node.method_name)
127123
end
128-
# rubocop:enable Metrics/CyclomaticComplexity
129124

130125
def lifecycle_hook_method?(node)
131126
node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)
132127
end
128+
129+
def assertion_prefix_method?(node)
130+
return false if node.receiver
131+
132+
method_name = node.method_name
133+
ASSERTION_PREFIXES.any? { |prefix| method_name.start_with?(prefix.to_s) }
134+
end
133135
end
134136
end
135137
end

test/rubocop/cop/minitest/multiple_assertions_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,16 @@ class FooTest < ActiveSupport::TestCase
745745
RUBY
746746
end
747747

748+
def test_does_not_register_offense_with_receiver
749+
assert_no_offenses(<<~RUBY)
750+
class FooTest < Minitest::Test
751+
def test_asserts_once
752+
assert_equal(foo, Bar.assert_something)
753+
end
754+
end
755+
RUBY
756+
end
757+
748758
private
749759

750760
def configure_max_assertions(max)

0 commit comments

Comments
 (0)