Class: RuboCop::Cop::Minitest::AssertRaisesWithRegexpArgument

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

Overview

Checks for ‘assert_raises` with arguments of regular expression literals. Arguments should be exception classes. Optionally the last argument can be a custom message string to help explain failures. Either way, it’s not the argument that ‘exception.message` is compared to. The raised exception is returned and can be used to match against a regular expression.

Examples:


# bad
assert_raises FooError, /some message/ do
  obj.occur_error
end

# good
exception = assert_raises FooError do
  obj.occur_error
end
assert_match(/some message/, exception.message)

Constant Summary collapse

MSG =
'Do not pass regular expression literals to `assert_raises`. Test the resulting exception.'
RESTRICT_ON_SEND =
%i[assert_raises].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



30
31
32
# File 'lib/rubocop/cop/minitest/assert_raises_with_regexp_argument.rb', line 30

def on_send(node)
  add_offense(node) if node.last_argument&.regexp_type?
end