class RuboCop::Cop::RSpec::UnspecifiedException

Checks for a specified error in checking raised errors.

Enforces one of an Exception type, a string, or a regular expression to match against the exception message as a parameter to ‘raise_error`

@example

# bad
expect {
  raise StandardError.new('error')
}.to raise_error

# good
expect {
  raise StandardError.new('error')
}.to raise_error(StandardError)

expect {
  raise StandardError.new('error')
}.to raise_error('error')

expect {
  raise StandardError.new('error')
}.to raise_error(/err/)

expect { do_something }.not_to raise_error

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/unspecified_exception.rb, line 46
def on_send(node)
  return unless empty_exception_matcher?(node)

  add_offense(node)
end

Private Instance Methods

empty_exception_matcher?(node) click to toggle source
# File lib/rubocop/cop/rspec/unspecified_exception.rb, line 54
def empty_exception_matcher?(node)
  return false if node.arguments? || node.block_literal?

  expect_to = find_expect_to(node)
  return false unless expect_to
  return false if expect_to.block_node&.arguments?

  true
end
find_expect_to(node) click to toggle source
# File lib/rubocop/cop/rspec/unspecified_exception.rb, line 64
def find_expect_to(node)
  node.each_ancestor.find do |ancestor|
    break if ancestor.block_type?
    next unless ancestor.send_type?

    expect_to?(ancestor)
  end
end