Sha256: 545cc58c27bdaa21e44fed2a3fe0a3a2c31fc62b084be692cb716bf21292038e

Contents?: true

Size: 723 Bytes

Versions: 2

Compression:

Stored size: 723 Bytes

Contents

module Matchi
  # **Expecting errors** matcher.
  class RaiseException < BasicObject
    # Initialize the matcher with a descendant of class Exception.
    #
    # @example Divided by 0 matcher
    #   Matchi::RaiseException.new(ZeroDivisionError)
    #
    # @param [.exception] expected the class of the expected exception
    def initialize(expected)
      @expected = expected
    end

    # @example Is it raising NameError?
    #   raise_exception = Matchi::RaiseException.new(NameError)
    #   raise_exception.matches? { Boom } # => true
    #
    # @return [Boolean] Comparison between actual and expected values.
    def matches?
      yield
    rescue @expected
      true
    else
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
matchi-0.0.5 lib/matchi/raise_exception.rb
matchi-0.0.4 lib/matchi/raise_exception.rb