Skip to content

Commit b11fd1c

Browse files
authored
Merge pull request #285 from koic/fix_an_error_for_minitest_multiple_assertions
[Fix #283] Fix an error for `Minitest/MultipleAssertions`
2 parents 9335ecb + fba823a commit b11fd1c

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#283](https://github.com/rubocop/rubocop-minitest/issues/283): Fix an error for `Minitest/MultipleAssertions` when using `||` assigning a value to a variable. ([@koic][])

lib/rubocop/cop/mixin/minitest_exploration_helpers.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ def assertions_count(node)
9999
end
100100
end
101101

102-
# rubocop:disable Metrics/CyclomaticComplexity
102+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
103103
def assertion_method?(node)
104+
return false unless node
104105
return assertion_method?(node.expression) if node.assignment? && node.respond_to?(:expression)
105106
return false if !node.send_type? && !node.block_type? && !node.numblock_type?
106107

@@ -110,7 +111,7 @@ def assertion_method?(node)
110111
method_name.start_with?(prefix) || node.method?(:flunk)
111112
end
112113
end
113-
# rubocop:enable Metrics/CyclomaticComplexity
114+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
114115

115116
def lifecycle_hook_method?(node)
116117
node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)

test/rubocop/cop/minitest/multiple_assertions_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def test_asserts_once
113113
RUBY
114114
end
115115

116+
def test_does_not_register_offense_when_using_or_assigning_a_value_to_an_object_attribute
117+
assert_no_offenses(<<~RUBY)
118+
class FooTest < Minitest::Test
119+
def test_asserts_once
120+
var ||= :value
121+
122+
assert_equal(foo, bar)
123+
end
124+
end
125+
RUBY
126+
end
127+
116128
def test_generates_a_todo_based_on_the_worst_violation
117129
inspect_source(<<-RUBY, @cop, 'test/foo_test.rb')
118130
class FooTest < Minitest::Test

0 commit comments

Comments
 (0)