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

Methods inherited from BaseMatcher

#and, #or

Constructor Details

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

Returns a new instance of RaiseErrorMatcher.



190
191
192
193
194
195
# File 'lib/crspec/matchers.rb', line 190

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



209
210
211
212
213
214
215
# File 'lib/crspec/matchers.rb', line 209

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



217
218
219
# File 'lib/crspec/matchers.rb', line 217

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

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


197
198
199
200
201
202
203
204
205
206
207
# File 'lib/crspec/matchers.rb', line 197

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