class RSpec::Matchers::BuiltIn::RaiseError
@api private Provides the implementation for `raise_error`. Not intended to be instantiated directly. rubocop:disable ClassLength
Public Class Methods
new(expected_error_or_message=Exception, expected_message=nil, &block)
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 11 def initialize(expected_error_or_message=Exception, expected_message=nil, &block) @block = block @actual_error = nil case expected_error_or_message when String, Regexp @expected_error, @expected_message = Exception, expected_error_or_message else @expected_error, @expected_message = expected_error_or_message, expected_message end end
Public Instance Methods
description()
click to toggle source
@api private @return [String]
# File lib/rspec/matchers/built_in/raise_error.rb, line 88 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 60 def does_not_match?(given_proc) prevent_invalid_expectations !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 70 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 76 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 82 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 32 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) @raised_expected_error = true @with_expected_message = verify_message end end unless negative_expectation eval_block if @raised_expected_error && @with_expected_message && @block end expectation_matched? end
supports_block_expectations?()
click to toggle source
@private
# File lib/rspec/matchers/built_in/raise_error.rb, line 66 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 24 def with_message(expected_message) raise_message_already_set if @expected_message @expected_message = expected_message self end
Private Instance Methods
block_matches?()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 102 def block_matches? @eval_block ? @eval_block_passed : true end
error_and_message_match?()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 98 def error_and_message_match? @raised_expected_error && @with_expected_message end
eval_block()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 106 def eval_block @eval_block = true begin @block[@actual_error] @eval_block_passed = true rescue Exception => err @actual_error = err end end
expectation_matched?()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 94 def expectation_matched? error_and_message_match? && block_matches? end
expected_error()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 136 def expected_error case @expected_message when nil description_of(@expected_error) when Regexp "#{@expected_error} with message matching #{@expected_message.inspect}" else "#{@expected_error} with #{description_of @expected_message}" end end
expecting_specific_exception?()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 163 def expecting_specific_exception? @expected_error != Exception end
format_backtrace(backtrace)
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 147 def format_backtrace(backtrace) formatter = Matchers.configuration.backtrace_formatter formatter.format_backtrace(backtrace) end
given_error()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 152 def given_error return " but was not given a block" unless Proc === @given_proc return " but nothing was raised" unless @actual_error backtrace = format_backtrace(@actual_error.backtrace) [ ", got #{@actual_error.inspect} with backtrace:", *backtrace ].join("\n # ") end
prevent_invalid_expectations()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 121 def prevent_invalid_expectations what_to_raise = if expecting_specific_exception? && @expected_message "`expect { }.not_to raise_error(SpecificErrorClass, message)`" elsif expecting_specific_exception? "`expect { }.not_to raise_error(SpecificErrorClass)`" elsif @expected_message "`expect { }.not_to raise_error(message)`" end return unless what_to_raise specific_class_error = ArgumentError.new("#{what_to_raise} is not valid, use `expect { }.not_to raise_error` (with no args) instead") raise specific_class_error end
raise_message_already_set()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 167 def raise_message_already_set raise "`expect { }.to raise_error(message).with_message(message)` is not valid. The matcher only allows the expected message to be specified once" end
verify_message()
click to toggle source
# File lib/rspec/matchers/built_in/raise_error.rb, line 116 def verify_message return true if @expected_message.nil? values_match?(@expected_message, @actual_error.message.to_s) end