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

Boolean comparison between the actual value and the expected value.

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.



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

def matches?
  yield
rescue @expected
  true
else
  false
end

#to_hHash

Returns a hash of one key-value pair with a key corresponding to the

matcher and a value corresponding to its initialize parameters.

Examples:

A FooBar matcher serialized into a hash.

matcher = Matchi::FooBar.new(42)
matcher.to_h # => { FooBar: 42 }

Returns:

  • (Hash)

    A hash of one key-value pair.



50
51
52
# File 'lib/matchi/raise_exception.rb', line 50

def to_h
  { RaiseException: [@expected] }
end

#to_sString

Returns a string representing the matcher.

Examples:

The readable definition of a FooBar matcher.

matcher = Matchi::FooBar.new(42)
matcher.to_s # => "foo_bar 42"

Returns:

  • (String)

    A string representing the matcher.



38
39
40
# File 'lib/matchi/raise_exception.rb', line 38

def to_s
  "raise_exception #{@expected.inspect}"
end