class RSpec::Matchers::BuiltIn::RaiseError

@api private Provides the implementation for ‘raise_error`. Not intended to be instantiated directly. rubocop:disable Metrics/ClassLength rubocop:disable Lint/RescueException

Constants

UndefinedValue

Used as a sentinel value to be able to tell when the user did not pass an argument. We can’t use ‘nil` for that because we need to warn when `nil` is passed in a different way. It’s an Object, not a Module, since Module’s ‘===` does not evaluate to true when compared to itself.

Note; this is the default value supplied for expected_error_or_message, but because there are two method-calls involved, that default is actually supplied in the definition of the matcher method, ‘RSpec::Matchers#raise_error`

Public Class Methods

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 22
def initialize(expected_error_or_message, expected_message, &block)
  @block = block
  @actual_error = nil
  @warn_about_bare_error = UndefinedValue === expected_error_or_message
  @warn_about_nil_error = expected_error_or_message.nil?

  case expected_error_or_message
  when nil, UndefinedValue
    @expected_error = Exception
    @expected_message = expected_message
  when String, Regexp
    @expected_error = Exception
    @expected_message = expected_error_or_message
  else
    @expected_error = expected_error_or_message
    @expected_message = expected_message
  end
end

Public Instance Methods

@api private @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 117
def description
  "raise #{expected_error}"
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 83
def does_not_match?(given_proc)
  warn_for_negative_false_positives!
  !matches?(given_proc, :negative_expectation) && Proc === given_proc
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 99
def expects_call_stack_jump?
  true
end

@api private @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 105
def failure_message
  @eval_block ? actual_error_message : "expected #{expected_error}#{given_error}"
end

@api private @return [String]

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 111
def failure_message_when_negated
  "expected no #{expected_error}#{given_error}"
end

rubocop:disable Metrics/MethodLength @private

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 52
def matches?(given_proc, negative_expectation=false, &block)
  @given_proc = given_proc
  @block ||= block
  @raised_expected_error = false
  @with_expected_message = false
  @eval_block = false
  @eval_block_passed = false

  return false unless Proc === given_proc

  begin
    given_proc.call
  rescue Exception => @actual_error
    if values_match?(@expected_error, @actual_error) ||
       values_match?(@expected_error, actual_error_message)
      @raised_expected_error = true
      @with_expected_message = verify_message
    end
  end

  unless negative_expectation
    warn_about_bare_error! if warn_about_bare_error?
    warn_about_nil_error! if warn_about_nil_error?
    eval_block if ready_to_eval_block?
  end

  expectation_matched?
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 89
def supports_block_expectations?
  true
end

@private

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 94
def supports_value_expectations?
  false
end

@api public Specifies the expected error message.

# File rspec-expectations/lib/rspec/matchers/built_in/raise_error.rb, line 43
def with_message(expected_message)
  raise_message_already_set if @expected_message
  @warn_about_bare_error = false
  @expected_message = expected_message
  self
end