Module: Mocha::ParameterMatchers

### Home Classes Methods

### In Files

- lib/mocha/parameter_matchers.rb
- lib/mocha/parameter_matchers/all_of.rb
- lib/mocha/parameter_matchers/any_of.rb
- lib/mocha/parameter_matchers/any_parameters.rb
- lib/mocha/parameter_matchers/anything.rb
- lib/mocha/parameter_matchers/base.rb
- lib/mocha/parameter_matchers/equals.rb
- lib/mocha/parameter_matchers/has_entries.rb
- lib/mocha/parameter_matchers/has_entry.rb
- lib/mocha/parameter_matchers/has_key.rb
- lib/mocha/parameter_matchers/has_value.rb
- lib/mocha/parameter_matchers/includes.rb
- lib/mocha/parameter_matchers/instance_of.rb
- lib/mocha/parameter_matchers/is_a.rb
- lib/mocha/parameter_matchers/kind_of.rb
- lib/mocha/parameter_matchers/not.rb
- lib/mocha/parameter_matchers/optionally.rb
- lib/mocha/parameter_matchers/query_string.rb
- lib/mocha/parameter_matchers/regexp_matches.rb
- lib/mocha/parameter_matchers/responds_with.rb
- lib/mocha/parameter_matchers/yaml_equivalent.rb

### Namespace

- CLASS Mocha::ParameterMatchers::AllOf
- CLASS Mocha::ParameterMatchers::AnyOf
- CLASS Mocha::ParameterMatchers::AnyParameters
- CLASS Mocha::ParameterMatchers::Anything
- CLASS Mocha::ParameterMatchers::Base
- CLASS Mocha::ParameterMatchers::Equals
- CLASS Mocha::ParameterMatchers::HasEntries
- CLASS Mocha::ParameterMatchers::HasEntry
- CLASS Mocha::ParameterMatchers::HasKey
- CLASS Mocha::ParameterMatchers::HasValue
- CLASS Mocha::ParameterMatchers::Includes
- CLASS Mocha::ParameterMatchers::InstanceOf
- CLASS Mocha::ParameterMatchers::IsA
- CLASS Mocha::ParameterMatchers::KindOf
- CLASS Mocha::ParameterMatchers::Not
- CLASS Mocha::ParameterMatchers::Optionally
- CLASS Mocha::ParameterMatchers::QueryStringMatches
- CLASS Mocha::ParameterMatchers::RegexpMatches
- CLASS Mocha::ParameterMatchers::RespondsWith
- CLASS Mocha::ParameterMatchers::YamlEquivalent

### Methods

- #Not
- #all_of
- #any_of
- #any_parameters
- #anything
- #equals
- #has_entries
- #has_entry
- #has_equivalent_query_string
- #has_key
- #has_value
- #includes
- #instance_of
- #is_a
- #kind_of
- #optionally
- #regexp_matches
- #responds_with
- #yaml_equivalent

### Class/Module Index

Quicksearch

- Mocha
- Mocha::API
- Mocha::AnyInstanceMethod
- Mocha::AnyInstanceReceiver
- Mocha::ArgumentIterator
- Mocha::ArrayMethods
- Mocha::BacktraceFilter
- Mocha::Cardinality
- Mocha::Central
- Mocha::ChangeStateSideEffect
- Mocha::ClassMethod
- Mocha::ClassMethods
- Mocha::ClassMethods::AnyInstance
- Mocha::Configuration
- Mocha::DateMethods
- Mocha::Debug
- Mocha::DefaultName
- Mocha::DefaultReceiver
- Mocha::Deprecation
- Mocha::Detection
- Mocha::Detection::MiniTest
- Mocha::Detection::TestUnit
- Mocha::ExceptionRaiser
- Mocha::Expectation
- Mocha::ExpectationError
- Mocha::ExpectationErrorFactory
- Mocha::ExpectationList
- Mocha::HashMethods
- Mocha::Hooks
- Mocha::ImpersonatingAnyInstanceName
- Mocha::ImpersonatingName
- Mocha::InStateOrderingConstraint
- Mocha::InstanceMethod
- Mocha::Integration
- Mocha::Integration::AssertionCounter
- Mocha::Integration::MiniTest
- Mocha::Integration::MiniTest::Adapter
- Mocha::Integration::MiniTest::Nothing
- Mocha::Integration::MiniTest::Version13
- Mocha::Integration::MiniTest::Version13::RunMethodPatch
- Mocha::Integration::MiniTest::Version140
- Mocha::Integration::MiniTest::Version140::RunMethodPatch
- Mocha::Integration::MiniTest::Version141
- Mocha::Integration::MiniTest::Version141::RunMethodPatch
- Mocha::Integration::MiniTest::Version142To172
- Mocha::Integration::MiniTest::Version142To172::RunMethodPatch
- Mocha::Integration::MiniTest::Version200
- Mocha::Integration::MiniTest::Version200::RunMethodPatch
- Mocha::Integration::MiniTest::Version201To222
- Mocha::Integration::MiniTest::Version201To222::RunMethodPatch
- Mocha::Integration::MiniTest::Version2110To2111
- Mocha::Integration::MiniTest::Version2110To2111::RunMethodPatch
- Mocha::Integration::MiniTest::Version2112To320
- Mocha::Integration::MiniTest::Version2112To320::RunMethodPatch
- Mocha::Integration::MiniTest::Version230To2101
- Mocha::Integration::MiniTest::Version230To2101::RunMethodPatch
- Mocha::Integration::MonkeyPatcher
- Mocha::Integration::TestUnit
- Mocha::Integration::TestUnit::Adapter
- Mocha::Integration::TestUnit::GemVersion200
- Mocha::Integration::TestUnit::GemVersion200::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion201To202
- Mocha::Integration::TestUnit::GemVersion201To202::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion203To220
- Mocha::Integration::TestUnit::GemVersion203To220::RunMethodPatch
- Mocha::Integration::TestUnit::GemVersion230To250
- Mocha::Integration::TestUnit::GemVersion230To250::RunMethodPatch
- Mocha::Integration::TestUnit::Nothing
- Mocha::Integration::TestUnit::RubyVersion185AndBelow
- Mocha::Integration::TestUnit::RubyVersion185AndBelow::RunMethodPatch
- Mocha::Integration::TestUnit::RubyVersion186AndAbove
- Mocha::Integration::TestUnit::RubyVersion186AndAbove::RunMethodPatch
- Mocha::Logger
- Mocha::MethodMatcher
- Mocha::Mock
- Mocha::Mockery
- Mocha::ModuleMethod
- Mocha::ModuleMethods
- Mocha::MultipleYields
- Mocha::Name
- Mocha::NoYields
- Mocha::ObjectMethods
- Mocha::ObjectReceiver
- Mocha::ParameterMatchers
- Mocha::ParameterMatchers::AllOf
- Mocha::ParameterMatchers::AnyOf
- Mocha::ParameterMatchers::AnyParameters
- Mocha::ParameterMatchers::Anything
- Mocha::ParameterMatchers::Base
- Mocha::ParameterMatchers::Equals
- Mocha::ParameterMatchers::HasEntries
- Mocha::ParameterMatchers::HasEntry
- Mocha::ParameterMatchers::HasKey
- Mocha::ParameterMatchers::HasValue
- Mocha::ParameterMatchers::Includes
- Mocha::ParameterMatchers::InstanceOf
- Mocha::ParameterMatchers::IsA
- Mocha::ParameterMatchers::KindOf
- Mocha::ParameterMatchers::Not
- Mocha::ParameterMatchers::Optionally
- Mocha::ParameterMatchers::QueryStringMatches
- Mocha::ParameterMatchers::RegexpMatches
- Mocha::ParameterMatchers::RespondsWith
- Mocha::ParameterMatchers::YamlEquivalent
- Mocha::ParametersMatcher
- Mocha::PrettyParameters
- Mocha::ReturnValues
- Mocha::Sequence
- Mocha::Sequence::InSequenceOrderingConstraint
- Mocha::SingleReturnValue
- Mocha::SingleYield
- Mocha::StateMachine
- Mocha::StateMachine::State
- Mocha::StateMachine::StatePredicate
- Mocha::StringMethods
- Mocha::StubbingError
- Mocha::Thrower
- Mocha::TimeMethods
- Mocha::UnexpectedInvocation
- Mocha::YieldParameters
- Array
- Date
- Hash
- Object
- String
- Time

No matching classes.

# Mocha::ParameterMatchers

Used as parameters for {Expectation#with} to restrict the parameter values which will match the expectation. Can be nested.

### Public Instance Methods

Not(matcher) click to toggle source

Matches if `matcher` does not match.

@param [Base] matcher matcher whose logic to invert. @return [Not] parameter matcher.

@see Expectation#with

@example Actual parameter does not include the value `1`.

    object = mock()
    object.expects(:method_1).with(Not(includes(1)))
    object.method_1([0, 2, 3])
    # no error raised

@example Actual parameter does include the value `1`.

    object = mock()
    object.expects(:method_1).with(Not(includes(1)))
    object.method_1([0, 1, 2, 3])
    # error raised, because method_1 was not called with object not including 1

    # File lib/mocha/parameter_matchers/not.rb, line 25
    def Not(matcher)
      Not.new(matcher)
    end

all_of(*matchers) click to toggle source

Matches if all `matchers` match.

@param [*Array<Base>] parameter_matchers parameter matchers. @return [AllOf] parameter matcher.

@see Expectation#with

@example All parameter matchers match.

    object = mock()
    object.expects(:method_1).with(all_of(includes(1), includes(3)))
    object.method_1([1, 3])
    # no error raised

@example One of the parameter matchers does not match.

    object = mock()
    object.expects(:method_1).with(all_of(includes(1), includes(3)))
    object.method_1([1, 2])
    # error raised, because method_1 was not called with object including 1 and 3

    # File lib/mocha/parameter_matchers/all_of.rb, line 25
    def all_of(*matchers)
      AllOf.new(*matchers)
    end

any_of(*matchers) click to toggle source

Matches if any `matchers` match.

@param [*Array<Base>] parameter_matchers parameter matchers. @return [AnyOf] parameter matcher.

@see Expectation#with

@example One parameter matcher matches.

    object = mock()
    object.expects(:method_1).with(any_of(1, 3))
    object.method_1(1)
    # no error raised

@example The other parameter matcher matches.

    object = mock()
    object.expects(:method_1).with(any_of(1, 3))
    object.method_1(3)
    # no error raised

@example Neither parameter matcher matches.

    object = mock()
    object.expects(:method_1).with(any_of(1, 3))
    object.method_1(2)
    # error raised, because method_1 was not called with 1 or 3

    # File lib/mocha/parameter_matchers/any_of.rb, line 31
    def any_of(*matchers)
      AnyOf.new(*matchers)
    end

any_parameters() click to toggle source

Matches any parameters. This is used as the default for a newly built expectation.

@return [AnyParameters] parameter matcher.

@see Expectation#with

@example Any parameters will match.

    object = mock()
    object.expects(:method_1).with(any_parameters)
    object.method_1(1, 2, 3, 4)
    # no error raised

    object = mock()
    object.expects(:method_1).with(any_parameters)
    object.method_1(5, 6, 7, 8, 9, 0)
    # no error raised

    # File lib/mocha/parameter_matchers/any_parameters.rb, line 23
    def any_parameters
      AnyParameters.new
    end

anything() click to toggle source

Matches any object.

@return [Anything] parameter matcher.

@see Expectation#with

@example Any object will match.

    object = mock()
    object.expects(:method_1).with(anything)
    object.method_1('foo')
    object.method_1(789)
    object.method_1(:bar)
    # no error raised

    # File lib/mocha/parameter_matchers/anything.rb, line 20
    def anything
      Anything.new
    end

equals(value) click to toggle source

Matches any `Object` equalling `value`.

@param [Object] value expected value. @return [Equals] parameter matcher.

@see Expectation#with @see Object#==

@example Actual parameter equals expected parameter.

    object = mock()
    object.expects(:method_1).with(equals(2))
    object.method_1(2)
    # no error raised

@example Actual parameter does not equal expected parameter.

    object = mock()
    object.expects(:method_1).with(equals(2))
    object.method_1(3)
    # error raised, because method_1 was not called with an +Object+ that equals 3

    # File lib/mocha/parameter_matchers/equals.rb, line 26
    def equals(value)
      Equals.new(value)
    end

has_entries(entries) click to toggle source

Matches `Hash` containing all `entries`.

@param [Hash] entries expected `Hash` entries. @return [HasEntries] parameter matcher.

@see Expectation#with

@example Actual parameter contains all expected entries.

    object = mock()
    object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
    object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
    # no error raised

@example Actual parameter does not contain all expected entries.

    object = mock()
    object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
    object.method_1('key_1' => 1, 'key_2' => 99)
    # error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2

    # File lib/mocha/parameter_matchers/has_entries.rb, line 27
    def has_entries(entries)
      HasEntries.new(entries)
    end

has_entry(*options) click to toggle source

Matches `Hash` containing entry with `key` and `value`.

@overload def has_entry(key, value)

    @param [Object] key key for entry.
    @param [Object] value value for entry.

@overload def has_entry(single_entry_hash)

    @param [Hash] single_entry_hash +Hash+ with single entry.
    @raise [ArgumentError] if +single_entry_hash+ does not contain exactly one entry.

@return [HasEntry] parameter matcher.

@see Expectation#with

@example Actual parameter contains expected entry supplied as key and value.

    object = mock()
    object.expects(:method_1).with(has_entry('key_1', 1))
    object.method_1('key_1' => 1, 'key_2' => 2)
    # no error raised

@example Actual parameter contains expected entry supplied as `Hash` entry.

    object = mock()
    object.expects(:method_1).with(has_entry('key_1' => 1))
    object.method_1('key_1' => 1, 'key_2' => 2)
    # no error raised

@example Actual parameter does not contain expected entry supplied as key and value.

    object = mock()
    object.expects(:method_1).with(has_entry('key_1', 1))
    object.method_1('key_1' => 2, 'key_2' => 1)
    # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1

@example Actual parameter does not contain expected entry supplied as `Hash` entry.

    object = mock()
    object.expects(:method_1).with(has_entry('key_1' => 1))
    object.method_1('key_1' => 2, 'key_2' => 1)
    # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1

    # File lib/mocha/parameter_matchers/has_entry.rb, line 44
    def has_entry(*options)
      case options.length
      when 1
        case options[0]
        when Hash
          case options[0].length
          when 0
            raise ArgumentError.new("Argument has no entries.")
          when 1
            key, value = options[0].first
          else
            raise ArgumentError.new("Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.")
          end
        else
          raise ArgumentError.new("Argument is not a Hash.")
        end
      when 2
        key, value = options
      else
        raise ArgumentError.new("Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).")
      end
      HasEntry.new(key, value)
    end

has_equivalent_query_string(uri) click to toggle source

Matches a URI without regard to the ordering of parameters in the query string.

@param [String] uri URI to match. @return [QueryStringMatches] parameter matcher.

@see Expectation#with

@example Actual URI has equivalent query string.

    object = mock()
    object.expects(:method_1).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2))
    object.method_1('http://example.com/foo?b=2&a=1')
    # no error raised

@example Actual URI does not have equivalent query string.

    object = mock()
    object.expects(:method_1).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2))
    object.method_1('http://example.com/foo?a=1&b=3')
    # error raised, because the query parameters were different

    # File lib/mocha/parameter_matchers/query_string.rb, line 25
    def has_equivalent_query_string(uri)
      QueryStringMatches.new(uri)
    end

has_key(key) click to toggle source

Matches `Hash` containing `key`.

@param [Object] key expected key. @return [HasKey] parameter matcher.

@see Expectation#with

@example Actual parameter contains entry with expected key.

    object = mock()
    object.expects(:method_1).with(has_key('key_1'))
    object.method_1('key_1' => 1, 'key_2' => 2)
    # no error raised

@example Actual parameter does not contain entry with expected key.

    object = mock()
    object.expects(:method_1).with(has_key('key_1'))
    object.method_1('key_2' => 2)
    # error raised, because method_1 was not called with Hash containing key: 'key_1'

    # File lib/mocha/parameter_matchers/has_key.rb, line 25
    def has_key(key)
      HasKey.new(key)
    end

has_value(value) click to toggle source

Matches `Hash` containing `value`.

@param [Object] value expected value. @return [HasValue] parameter matcher.

@see Expectation#with

@example Actual parameter contains entry with expected value.

    object = mock()
    object.expects(:method_1).with(has_value(1))
    object.method_1('key_1' => 1, 'key_2' => 2)
    # no error raised

@example Actual parameter does not contain entry with expected value.

    object = mock()
    object.expects(:method_1).with(has_value(1))
    object.method_1('key_2' => 2)
    # error raised, because method_1 was not called with Hash containing value: 1

    # File lib/mocha/parameter_matchers/has_value.rb, line 25
    def has_value(value)
      HasValue.new(value)
    end

includes(*items) click to toggle source

Matches any object that responds with `true` to +include?(item)+ for all items.

@param [*Array] items expected items. @return [Includes] parameter matcher.

@see Expectation#with

@example Actual parameter includes all items.

    object = mock()
    object.expects(:method_1).with(includes('foo', 'bar'))
    object.method_1(['foo', 'bar', 'baz'])
    # no error raised

@example Actual parameter does not include all items.

    object.method_1(['foo', 'baz'])
    # error raised, because ['foo', 'baz'] does not include 'bar'.

    # File lib/mocha/parameter_matchers/includes.rb, line 25
    def includes(*items)
      Includes.new(*items)
    end

instance_of(klass) click to toggle source

Matches any object that is an instance of `klass`

@param [Class] klass expected class. @return [InstanceOf] parameter matcher.

@see Expectation#with @see Kernel#instance_of?

@example Actual parameter is an instance of `String`.

    object = mock()
    object.expects(:method_1).with(instance_of(String))
    object.method_1('string')
    # no error raised

@example Actual parameter is not an instance of `String`.

    object = mock()
    object.expects(:method_1).with(instance_of(String))
    object.method_1(99)
    # error raised, because method_1 was not called with an instance of String

    # File lib/mocha/parameter_matchers/instance_of.rb, line 26
    def instance_of(klass)
      InstanceOf.new(klass)
    end

is_a(klass) click to toggle source

Matches any object that is a `klass`.

@param [Class] klass expected class. @return [IsA] parameter matcher.

@see Expectation#with @see Kernel#is_a?

@example Actual parameter is a `Integer`.

    object = mock()
    object.expects(:method_1).with(is_a(Integer))
    object.method_1(99)
    # no error raised

@example Actual parameter is not a `Integer`.

    object = mock()
    object.expects(:method_1).with(is_a(Integer))
    object.method_1('string')
    # error raised, because method_1 was not called with an Integer

    # File lib/mocha/parameter_matchers/is_a.rb, line 26
    def is_a(klass)
      IsA.new(klass)
    end

kind_of(klass) click to toggle source

Matches any `Object` that is a kind of `klass`.

@param [Class] klass expected class. @return [KindOf] parameter matcher.

@see Expectation#with @see Kernel#kind_of?

@example Actual parameter is a kind of `Integer`.

    object = mock()
    object.expects(:method_1).with(kind_of(Integer))
    object.method_1(99)
    # no error raised

@example Actual parameter is not a kind of `Integer`.

    object = mock()
    object.expects(:method_1).with(kind_of(Integer))
    object.method_1('string')
    # error raised, because method_1 was not called with a kind of Integer

    # File lib/mocha/parameter_matchers/kind_of.rb, line 26
    def kind_of(klass)
      KindOf.new(klass)
    end

optionally(*matchers) click to toggle source

Matches optional parameters if available.

@param [*Array<Base>] matchers matchers for optional parameters. @return [Optionally] parameter matcher.

@see Expectation#with

@example Only the two required parameters are supplied and they both match their expected value.

    object = mock()
    object.expects(:method_1).with(1, 2, optionally(3, 4))
    object.method_1(1, 2)
    # no error raised

@example Both required parameters and one of the optional parameters are supplied and they all match their expected value.

    object = mock()
    object.expects(:method_1).with(1, 2, optionally(3, 4))
    object.method_1(1, 2, 3)
    # no error raised

@example Both required parameters and both of the optional parameters are supplied and they all match their expected value.

    object = mock()
    object.expects(:method_1).with(1, 2, optionally(3, 4))
    object.method_1(1, 2, 3, 4)
    # no error raised

@example One of the actual optional parameters does not match the expected value.

    object = mock()
    object.expects(:method_1).with(1, 2, optionally(3, 4))
    object.method_1(1, 2, 3, 5)
    # error raised, because optional parameters did not match

    # File lib/mocha/parameter_matchers/optionally.rb, line 35
    def optionally(*matchers)
      Optionally.new(*matchers)
    end

regexp_matches(regexp) click to toggle source

Matches any object that matches `regexp`.

@param [Regexp] regexp regular expression to match. @return [RegexpMatches] parameter matcher.

@see Expectation#with

@example Actual parameter is matched by specified regular expression.

    object = mock()
    object.expects(:method_1).with(regexp_matches(/e/))
    object.method_1('hello')
    # no error raised

@example Actual parameter is not matched by specified regular expression.

    object = mock()
    object.expects(:method_1).with(regexp_matches(/a/))
    object.method_1('hello')
    # error raised, because method_1 was not called with a parameter that matched the
    # regular expression

    # File lib/mocha/parameter_matchers/regexp_matches.rb, line 26
    def regexp_matches(regexp)
      RegexpMatches.new(regexp)
    end

responds_with(message, result) click to toggle source

Matches any object that responds to `message` with `result`. To put it another way, it tests the quack, not the duck.

@param [Symbol] message method to invoke. @param [Object] result expected result of sending `message`. @return [RespondsWith] parameter matcher.

@see Expectation#with

@example Actual parameter responds with "FOO" when :upcase is invoked.

    object = mock()
    object.expects(:method_1).with(responds_with(:upcase, "FOO"))
    object.method_1("foo")
    # no error raised, because "foo".upcase == "FOO"

@example Actual parameter does not respond with "FOO" when :upcase is invoked.

    object = mock()
    object.expects(:method_1).with(responds_with(:upcase, "BAR"))
    object.method_1("foo")
    # error raised, because "foo".upcase != "BAR"

    # File lib/mocha/parameter_matchers/responds_with.rb, line 27
    def responds_with(message, result)
      RespondsWith.new(message, result)
    end

yaml_equivalent(object) click to toggle source

Matches any YAML that represents the specified `object`

@param [Object] object object whose YAML to compare. @return [YamlEquivalent] parameter matcher.

@see Expectation#with

@example Actual parameter is YAML equivalent of specified `object`.

    object = mock()
    object.expects(:method_1).with(yaml_equivalent(1, 2, 3))
    object.method_1("--- \n- 1\n- 2\n- 3\n")
    # no error raised

@example Actual parameter is not YAML equivalent of specified `object`.

    object = mock()
    object.expects(:method_1).with(yaml_equivalent(1, 2, 3))
    object.method_1("--- \n- 1\n- 2\n")
    # error raised, because method_1 was not called with YAML representing the specified Array

    # File lib/mocha/parameter_matchers/yaml_equivalent.rb, line 26
    def yaml_equivalent(object)
      YamlEquivalent.new(object)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.