Class: Smartest::RaiseErrorMatcher

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

Instance Method Summary collapse

Methods inherited from Matcher

#and, #or

Constructor Details

#initialize(*expected_error) ⇒ RaiseErrorMatcher

Returns a new instance of RaiseErrorMatcher.



528
529
530
531
532
533
534
# File 'lib/smartest/matchers.rb', line 528

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

#descriptionObject



561
562
563
# File 'lib/smartest/matchers.rb', line 561

def description
  "raise #{expected_description}"
end

#failure_messageObject



550
551
552
553
554
555
# File 'lib/smartest/matchers.rb', line 550

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)


536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/smartest/matchers.rb', line 536

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



557
558
559
# File 'lib/smartest/matchers.rb', line 557

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