Module: Mocha::ParameterMatchers::BaseMethods — Mocha 2.8.2

# Module: Mocha::ParameterMatchers::BaseMethods Abstract

Included in:
  AllOf, AnyOf, AnyParameters, Anything, Base, Equals, EquivalentUri, HasEntries, HasEntry, HasKey, HasKeys, HasValue, Includes, InstanceOf, IsA, KindOf, Not, Optionally, RegexpMatches, RespondsWith, YamlEquivalent

Defined in:
  lib/mocha/parameter_matchers/base.rb

## Overview

This module is abstract.

Include and implement `#matches?` and `#mocha_inspect` to define a custom matcher. Also add a suitably named instance method to Methods to build an instance of the new matcher c.f. Methods#equals.

## Instance Method Summary collapse

- #&(other) ⇒ AllOf

  A shorthand way of combining two matchers when both must match.

- #|(other) ⇒ AnyOf

  A shorthand way of combining two matchers when at least one must match.

## Instance Method Details

### #&(other) ⇒ `AllOf`

A shorthand way of combining two matchers when both must match.

Returns a new AllOf parameter matcher combining two matchers using a logical AND.

This shorthand will not work with an implicit equals match. Instead, an explicit Equals matcher should be used.

#### Examples:

#####

Alternative ways to combine matchers with a logical AND.

    object = mock()
    object.expects(:run).with(all_of(has_key(:foo), has_key(:bar)))
    object.run(foo: 'foovalue', bar: 'barvalue')

    # is exactly equivalent to

    object.expects(:run).with(has_key(:foo) & has_key(:bar))
    object.run(foo: 'foovalue', bar: 'barvalue)

Parameters:

- other (`BaseMethods`) —

  parameter matcher.

Returns:

- (`AllOf`) —

  parameter matcher.

See Also:

- Expectation#with

    28
    29
    30
    # File 'lib/mocha/parameter_matchers/base.rb', line 28

    def &(other)
      AllOf.new(self, other)
    end

### #|(other) ⇒ `AnyOf`

A shorthand way of combining two matchers when at least one must match.

Returns a new `AnyOf` parameter matcher combining two matchers using a logical OR.

This shorthand will not work with an implicit equals match. Instead, an explicit Equals matcher should be used.

#### Examples:

#####

Alternative ways to combine matchers with a logical OR.

    object = mock()
    object.expects(:run).with(any_of(has_key(:foo), has_key(:bar)))
    object.run(foo: 'foovalue')

    # is exactly equivalent to

    object.expects(:run).with(has_key(:foo) | has_key(:bar))
    object.run(foo: 'foovalue')

#####

Using an explicit Equals matcher in combination with #|.

    object.expects(:run).with(equals(1) | equals(2))
    object.run(1) # passes
    object.run(2) # passes
    object.run(3) # fails

Parameters:

- other (`BaseMethods`) —

  parameter matcher.

Returns:

- (`AnyOf`) —

  parameter matcher.

See Also:

- Expectation#with

    58
    59
    60
    # File 'lib/mocha/parameter_matchers/base.rb', line 58

    def |(other)
      AnyOf.new(self, other)
    end