class RSpec::Matchers::BuiltIn::RaiseError

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

Public Instance Methods

description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/raise_error.rb, line 97
def description
  "raise #{expected_error}"
end
does_not_match?(given_proc) click to toggle source

@private

# File lib/rspec/matchers/built_in/raise_error.rb, line 69
def does_not_match?(given_proc)
  warn_for_false_positives
  !matches?(given_proc, :negative_expectation) && Proc === given_proc
end
expects_call_stack_jump?() click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 79
def expects_call_stack_jump?
  true
end
failure_message() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/raise_error.rb, line 85
def failure_message
  @eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
end
failure_message_when_negated() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/raise_error.rb, line 91
def failure_message_when_negated
  "expected no #{expected_error}#{given_error}"
end
matches?(given_proc, negative_expectation=false, &block) click to toggle source

rubocop:disable MethodLength @private

# File lib/rspec/matchers/built_in/raise_error.rb, line 41
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

  warn_about_bare_error if warning_about_bare_error && !negative_expectation
  eval_block if !negative_expectation && ready_to_eval_block?

  expectation_matched?
end
supports_block_expectations?() click to toggle source

@private

# File lib/rspec/matchers/built_in/raise_error.rb, line 75
def supports_block_expectations?
  true
end
with_message(expected_message) click to toggle source

@api public Specifies the expected error message.

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

Public Class Methods

new(expected_error_or_message=nil, expected_message=nil, &block) click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 12
def initialize(expected_error_or_message=nil, expected_message=nil, &block)
  @block = block
  @actual_error = nil
  @warn_about_bare_error = expected_error_or_message.nil?

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