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.



151
152
153
154
155
# File 'lib/smartest/matchers.rb', line 151

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

Instance Method Details

#failure_messageObject



171
172
173
174
175
176
# File 'lib/smartest/matchers.rb', line 171

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)


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/smartest/matchers.rb', line 157

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



178
179
180
# File 'lib/smartest/matchers.rb', line 178

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