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.



78
79
80
81
82
# File 'lib/smartest/matchers.rb', line 78

def initialize(expected_error)
  @expected_error = expected_error
  @actual_error = nil
  @callable = true
end

Instance Method Details

#failure_messageObject



98
99
100
101
102
103
# File 'lib/smartest/matchers.rb', line 98

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

  "expected block to raise #{@expected_error}, but raised #{@actual_error.class}: #{@actual_error.message}"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/smartest/matchers.rb', line 84

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
  error.is_a?(@expected_error)
end

#negated_failure_messageObject



105
106
107
# File 'lib/smartest/matchers.rb', line 105

def negated_failure_message
  "expected block not to raise #{@expected_error}, but raised #{@actual_error.class}: #{@actual_error.message}"
end