Class: Matchi::BeInstanceOf

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

Overview

Type/class matcher.

Direct Known Subclasses

BeAnInstanceOf

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ BeInstanceOf

Initialize the matcher with an object.

Examples:

A string matcher

Matchi::BeInstanceOf.new(String)

Parameters:

  • expected (#object_id)

    An expected class.



11
12
13
# File 'lib/matchi/rspec/be_instance_of.rb', line 11

def initialize(expected)
  @expected = expected
end

Instance Method Details

#matches?Boolean

Returns Comparison between actual and expected values.

Examples:

Is it an instance of string?

be_instance_of = Matchi::BeInstanceOf.new(String)
be_instance_of.matches? { 'foo' } # => true

Yield Returns:

  • (#instance_of?)

    the actual value to compare to the expected one.

Returns:

  • (Boolean)

    Comparison between actual and expected values.



23
24
25
# File 'lib/matchi/rspec/be_instance_of.rb', line 23

def matches?
  yield.instance_of?(@expected)
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.

Returns:

  • (Hash)

    A hash of one key-value pair.



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

def to_h
  { BeInstanceOf: [@expected] }
end

#to_sString

Returns a string representing the matcher.

Returns:

  • (String)

    A string representing the matcher.



30
31
32
# File 'lib/matchi/rspec/be_instance_of.rb', line 30

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