Class: Matchi::RaiseException

Inherits:
BasicObject
Defined in:
lib/matchi/raise_exception.rb

Overview

**Expecting errors** matcher.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ RaiseException

Initialize the matcher with a descendant of class Exception.

Examples:

Divided by 0 matcher

Matchi::RaiseException.new(ZeroDivisionError)

Parameters:

  • expected (.exception)

    The class of the expected exception.



10
11
12
# File 'lib/matchi/raise_exception.rb', line 10

def initialize(expected)
  @expected = expected
end

Instance Method Details

#matches?Boolean

Returns Comparison between actual and expected values.

Examples:

Is it raising NameError?

raise_exception = Matchi::RaiseException.new(NameError)
raise_exception.matches? { Boom } # => true

Yield Returns:

  • (#object_id)

    the actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



21
22
23
24
25
26
27
# File 'lib/matchi/raise_exception.rb', line 21

def matches?
  yield
rescue @expected
  true
else
  false
end