Module: Mocha::ParameterMatchers

Module | Mocha::ParameterMatchers
In: | 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/regexp_matches.rb lib/mocha/parameter_matchers/responds_with.rb lib/mocha/parameter_matchers/yaml_equivalent.rb lib/mocha/parameter_matchers.rb

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

### Methods

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

### Public Instance methods

Not(parameter_matcher) → parameter_matcher

Matches if `parameter_matcher` does not match.

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

      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

all_of(*parameter_matchers) → parameter_matcher

Matches if 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

      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

any_of(*parameter_matchers) → parameter_matcher

Matches if any `parameter_matchers` match.

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

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

      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

any_parameters() → parameter_matcher

Matches any parameters.

      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

anything() → parameter_matcher

Matches any object.

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

equals(value) → parameter_matcher

Matches `Object` equalling `value`.

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

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

has_entries(entries) → parameter_matcher

Matches `Hash` containing all `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

      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

has_entry(key, value) → parameter_matcher
has_entry(key => value) → parameter_matcher

Matches `Hash` containing entry with `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

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

      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

      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

has_key(key) → parameter_matcher

Matches `Hash` containing `key`.

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

      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'

has_value(value) → parameter_matcher

Matches `Hash` containing `value`.

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

      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

includes(item) → parameter_matcher

Matches any object that responds true to include?(item)

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

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

instance_of(klass) → parameter_matcher

Matches any object that is an instance of `klass`

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

      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

is_a(klass) → parameter_matcher

Matches any object that is a `klass`

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

      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

kind_of(klass) → parameter_matcher

Matches any object that is a kind of `klass`

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

      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

optionally(*parameter_matchers) → parameter_matcher

Matches optional parameters if available.

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

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

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

      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

regexp_matches(regular_expression) → parameter_matcher

Matches any object that matches `regular_expression`.

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

      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

responds_with(message, result) → parameter_matcher

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

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

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

yaml_equivalent(object) → parameter_matcher

Matches any YAML that represents the 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

      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

[Validate]