Class: Matchi::Matcher::RaiseException

Inherits:
Base
  • Object
show all
Defined in:
lib/matchi/matcher/raise_exception.rb

Overview

**Expecting errors** matcher.

Instance Attribute Summary

Attributes inherited from Base

#expected

Instance Method Summary collapse

Methods inherited from Base

#inspect, #to_s, to_sym

Constructor Details

#initialize(expected) ⇒ RaiseException

Initialize the matcher with a descendant of class Exception.

Examples:

Divided by 0 matcher.

Matchi::Matcher::RaiseException.new(ZeroDivisionError)

Parameters:

  • expected (Exception)

    The class of the expected exception.



15
16
17
18
# File 'lib/matchi/matcher/raise_exception.rb', line 15

def initialize(expected)
  super()
  @expected = expected
end

Instance Method Details

#matches?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

Is it raising NameError?

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

Yield Returns:

  • (#object_id)

    The actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



30
31
32
33
34
35
36
# File 'lib/matchi/matcher/raise_exception.rb', line 30

def matches?(*, **)
  yield
rescue expected => _e
  true
else
  false
end