diff --git a/changelog/fix_false_positive_for_empty_line_before_assertion_methods_and_assert_raises.md b/changelog/fix_false_positive_for_empty_line_before_assertion_methods_and_assert_raises.md new file mode 100644 index 0000000..4a2cf5c --- /dev/null +++ b/changelog/fix_false_positive_for_empty_line_before_assertion_methods_and_assert_raises.md @@ -0,0 +1 @@ +* [#271](https://github.com/rubocop/rubocop-minitest/issues/271): Fix a false positive for `Minitest/EmptyLineBeforeAssertionMethods` and `assert_raises`. ([@fatkodima][]) diff --git a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb index 9bffbd2..efd9f8b 100644 --- a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +++ b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb @@ -100,6 +100,7 @@ def assertions_count(node) end def assertion_method?(node) + return assertion_method?(node.expression) if node.assignment? return false if !node.send_type? && !node.block_type? && !node.numblock_type? ASSERTION_PREFIXES.any? do |prefix| diff --git a/test/rubocop/cop/minitest/empty_line_before_assertion_methods_test.rb b/test/rubocop/cop/minitest/empty_line_before_assertion_methods_test.rb index 598ec8d..49d4d1c 100644 --- a/test/rubocop/cop/minitest/empty_line_before_assertion_methods_test.rb +++ b/test/rubocop/cop/minitest/empty_line_before_assertion_methods_test.rb @@ -110,18 +110,30 @@ def test_do_something def test_registers_offense_when_using_method_call_with_block_arg_before_assertion_method assert_offense(<<~RUBY) def test_do_something - block = -> { raise CustomError, 'This is really bad' } - error = assert_raises(CustomError, &block) - assert_equal 'This is really bad', error.message - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion. + do_something do + do_something_more + end + assert_equal(expected, actual) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line before assertion. end RUBY assert_correction(<<~RUBY) + def test_do_something + do_something do + do_something_more + end + + assert_equal(expected, actual) + end + RUBY + end + + def test_does_not_register_offense_when_using_assertion_method_with_assignment_before_assertion_method + assert_no_offenses(<<~RUBY) def test_do_something block = -> { raise CustomError, 'This is really bad' } error = assert_raises(CustomError, &block) - assert_equal 'This is really bad', error.message end RUBY