Class: Smartest::RaiseErrorMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(*expected_error) ⇒ RaiseErrorMatcher

Returns a new instance of RaiseErrorMatcher.



373
374
375
376
377
378
379
# File 'lib/smartest/matchers.rb', line 373

def initialize(*expected_error)
  @expected_type = expected_type_for(expected_error)
  @expected_error_class = expected_error.find { |item| error_class?(item) }
  @expected_message_regexp = expected_error.find { |item| item.is_a?(Regexp) }
  @actual_error = nil
  @callable = true
end

Instance Method Details

#failure_messageObject



395
396
397
398
399
400
# File 'lib/smartest/matchers.rb', line 395

def failure_message
  return "expected a block to raise #{expected_description}" unless @callable
  return "expected block to raise #{expected_description}, but nothing was raised" unless @actual_error

  "expected block to raise #{expected_description}, but raised #{actual_error_description}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/smartest/matchers.rb', line 381

def matches?(actual)
  @actual_error = nil
  @callable = actual.respond_to?(:call)
  return false unless @callable

  actual.call
  false
rescue Exception => error
  raise if Smartest.fatal_exception?(error)

  @actual_error = error
  expected_error_matches?(error)
end

#negated_failure_messageObject



402
403
404
# File 'lib/smartest/matchers.rb', line 402

def negated_failure_message
  "expected block not to raise #{expected_description}, but raised #{actual_error_description}"
end