Class: Crspec::Matchers::RaiseErrorMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/crspec/matchers.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected

Instance Method Summary collapse

Constructor Details

#initialize(expected_exception = StandardError, expected_message = nil) ⇒ RaiseErrorMatcher

Returns a new instance of RaiseErrorMatcher.



110
111
112
113
114
115
# File 'lib/crspec/matchers.rb', line 110

def initialize(expected_exception = StandardError, expected_message = nil)
  super(expected_exception)
  @expected_exception = expected_exception
  @expected_message = expected_message
  @actual_exception = nil
end

Instance Method Details

#failure_messageObject



129
130
131
132
133
134
135
# File 'lib/crspec/matchers.rb', line 129

def failure_message
  if @actual_exception
    "Expected #{@expected_exception}#{@expected_message ? " with message #{@expected_message.inspect}" : ""}, but got #{@actual_exception.class}: #{@actual_exception.message.inspect}"
  else
    "Expected #{@expected_exception} to be raised, but nothing was raised"
  end
end

#failure_message_when_negatedObject



137
138
139
# File 'lib/crspec/matchers.rb', line 137

def failure_message_when_negated
  "Expected no exception, but #{@actual_exception.class} was raised"
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/crspec/matchers.rb', line 117

def matches?(block)
  raise ArgumentError, "raise_error matcher requires a block" unless block.respond_to?(:call)

  begin
    block.call
    false
  rescue Exception => e
    @actual_exception = e
    matches_exception?(e)
  end
end