Class: RuboCop::Cop::Minitest::AssertWithExpectedArgument

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/minitest/assert_with_expected_argument.rb

Overview

Tries to detect when a user accidentally used ‘assert` when they meant to use `assert_equal`.

NOTE: The second argument to the ‘assert` method named `message` and `msg` is allowed.

Because their names are inferred as message arguments.

Examples:

# bad
assert(3, my_list.length)
assert(expected, actual)

# good
assert_equal(3, my_list.length)
assert_equal(expected, actual)
assert(foo, 'message')
assert(foo, message)
assert(foo, msg)

Constant Summary collapse

MSG =
'Did you mean to use `assert_equal(%<arguments>s)`?'
RESTRICT_ON_SEND =
%i[assert].freeze
MESSAGE_VARIABLES =
%w[message msg].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rubocop/cop/minitest/assert_with_expected_argument.rb', line 37

def on_send(node)
  assert_with_two_arguments?(node) do |_expected, message|
    return if message.str_type? || message.dstr_type? || MESSAGE_VARIABLES.include?(message.source)

    arguments = node.arguments.map(&:source).join(', ')
    add_offense(node, message: format(MSG, arguments: arguments))
  end
end