Class: Matchi::RaiseException

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

Overview

*Expecting errors* matcher.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ RaiseException

Initialize the matcher with a descendant of class Exception.

Examples:

require "matchi/raise_exception"

Matchi::RaiseException.new(NameError)

Parameters:

  • expected (Exception, #to_s)

    The expected exception name.



17
18
19
# File 'lib/matchi/raise_exception.rb', line 17

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

Instance Attribute Details

#expectedString (readonly)

Returns The expected exception name.

Returns:

  • (String)

    The expected exception name.



7
8
9
# File 'lib/matchi/raise_exception.rb', line 7

def expected
  @expected
end

Instance Method Details

#inspectObject

A string containing a human-readable representation of the matcher.



44
45
46
# File 'lib/matchi/raise_exception.rb', line 44

def inspect
  "#{self.class}(#{expected})"
end

#matches?Boolean

Boolean comparison between the actual value and the expected value.

Examples:

require "matchi/raise_exception"

matcher = Matchi::RaiseException.new(NameError)

matcher.expected          # => "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.



35
36
37
38
39
40
41
# File 'lib/matchi/raise_exception.rb', line 35

def matches?
  yield
rescue self.class.const_get(expected) => _e
  true
else
  false
end

#to_sObject

Returns a string representing the matcher.



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

def to_s
  "raise exception #{expected}"
end