Module: Mocha::ParameterMatchers

Included in:
API
Defined in:
lib/mocha/parameter_matchers.rb,
lib/mocha/parameter_matchers/not.rb,
lib/mocha/parameter_matchers/base.rb,
lib/mocha/parameter_matchers/is_a.rb,
lib/mocha/parameter_matchers/all_of.rb,
lib/mocha/parameter_matchers/any_of.rb,
lib/mocha/parameter_matchers/equals.rb,
lib/mocha/parameter_matchers/has_key.rb,
lib/mocha/parameter_matchers/kind_of.rb,
lib/mocha/parameter_matchers/includes.rb,
lib/mocha/parameter_matchers/anything.rb,
lib/mocha/parameter_matchers/has_value.rb,
lib/mocha/parameter_matchers/has_entry.rb,
lib/mocha/parameter_matchers/optionally.rb,
lib/mocha/parameter_matchers/has_entries.rb,
lib/mocha/parameter_matchers/instance_of.rb,
lib/mocha/parameter_matchers/query_string.rb,
lib/mocha/parameter_matchers/responds_with.rb,
lib/mocha/parameter_matchers/any_parameters.rb,
lib/mocha/parameter_matchers/regexp_matches.rb,
lib/mocha/parameter_matchers/yaml_equivalent.rb

Overview

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

Defined Under Namespace

Classes: AllOf, AnyOf, AnyParameters, Anything, Base, Equals, HasEntries, HasEntry, HasKey, HasValue, Includes, InstanceOf, IsA, KindOf, Not, Optionally, QueryStringMatches, RegexpMatches, RespondsWith, YamlEquivalent

Instance Method Summary (collapse)

Instance Method Details

- (AllOf) all_of(*matchers)

Matches if all matchers match.

Examples:

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

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

See Also:



25
26
27
# File 'lib/mocha/parameter_matchers/all_of.rb', line 25

def all_of(*matchers)
  AllOf.new(*matchers)
end

- (AnyOf) any_of(*matchers)

Matches if any matchers match.

Examples:

One parameter matcher matches.

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

The other parameter matcher matches.

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

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

See Also:



31
32
33
# File 'lib/mocha/parameter_matchers/any_of.rb', line 31

def any_of(*matchers)
  AnyOf.new(*matchers)
end

- (AnyParameters) any_parameters

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

Examples:

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

See Also:



23
24
25
# File 'lib/mocha/parameter_matchers/any_parameters.rb', line 23

def any_parameters
  AnyParameters.new
end

- (Anything) anything

Matches any object.

Examples:

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

See Also:



20
21
22
# File 'lib/mocha/parameter_matchers/anything.rb', line 20

def anything
  Anything.new
end

- (Equals) equals(value)

Matches any Object equalling value.

Examples:

Actual parameter equals expected parameter.

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

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

See Also:



26
27
28
# File 'lib/mocha/parameter_matchers/equals.rb', line 26

def equals(value)
  Equals.new(value)
end

- (HasEntries) has_entries(entries)

Matches Hash containing all entries.

Examples:

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

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

See Also:



27
28
29
# File 'lib/mocha/parameter_matchers/has_entries.rb', line 27

def has_entries(entries)
  HasEntries.new(entries)
end

- (HasEntry) has_entry(key, value) - (HasEntry) has_entry(single_entry_hash)

Matches Hash containing entry with key and value.

Examples:

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

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

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

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

Overloads:

  • - (HasEntry) has_entry(single_entry_hash)

    Raises:

    • (ArgumentError)

      if single_entry_hash does not contain exactly one entry.

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mocha/parameter_matchers/has_entry.rb', line 44

def has_entry(*options)
  key, value = options.shift, options.shift
  if key.is_a?(Hash)
    case key.length
    when 0
      raise ArgumentError.new("Argument has no entries.")
    when 1
      key, value = key.to_a.flatten
    else
      raise ArgumentError.new("Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.")
    end
  end
  HasEntry.new(key, value)
end

- (QueryStringMatches) has_equivalent_query_string(uri)

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

Examples:

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

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

See Also:



25
26
27
# File 'lib/mocha/parameter_matchers/query_string.rb', line 25

def has_equivalent_query_string(uri)
  QueryStringMatches.new(uri)
end

- (HasKey) has_key(key)

Matches Hash containing key.

Examples:

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

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'

See Also:



25
26
27
# File 'lib/mocha/parameter_matchers/has_key.rb', line 25

def has_key(key)
  HasKey.new(key)
end

- (HasValue) has_value(value)

Matches Hash containing value.

Examples:

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

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

See Also:



25
26
27
# File 'lib/mocha/parameter_matchers/has_value.rb', line 25

def has_value(value)
  HasValue.new(value)
end

- (Includes) includes(item)

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

Examples:

Actual parameter includes item.

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

Actual parameter does not include item.

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

See Also:



23
24
25
# File 'lib/mocha/parameter_matchers/includes.rb', line 23

def includes(item)
  Includes.new(item)
end

- (InstanceOf) instance_of(klass)

Matches any object that is an instance of klass

Examples:

Actual parameter is an instance of String.

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

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

See Also:



26
27
28
# File 'lib/mocha/parameter_matchers/instance_of.rb', line 26

def instance_of(klass)
  InstanceOf.new(klass)
end

- (IsA) is_a(klass)

Matches any object that is a klass.

Examples:

Actual parameter is a Integer.

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

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

See Also:



26
27
28
# File 'lib/mocha/parameter_matchers/is_a.rb', line 26

def is_a(klass)
  IsA.new(klass)
end

- (KindOf) kind_of(klass)

Matches any Object that is a kind of klass.

Examples:

Actual parameter is a kind of Integer.

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

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

See Also:



26
27
28
# File 'lib/mocha/parameter_matchers/kind_of.rb', line 26

def kind_of(klass)
  KindOf.new(klass)
end

- (Not) Not(matcher)

Matches if matcher does not match.

Examples:

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

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

See Also:



25
26
27
# File 'lib/mocha/parameter_matchers/not.rb', line 25

def Not(matcher)
  Not.new(matcher)
end

- (Optionally) optionally(*matchers)

Matches optional parameters if available.

Examples:

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

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

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

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

See Also:



35
36
37
# File 'lib/mocha/parameter_matchers/optionally.rb', line 35

def optionally(*matchers)
  Optionally.new(*matchers)
end

- (RegexpMatches) regexp_matches(regexp)

Matches any object that matches regexp.

Examples:

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

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

See Also:



26
27
28
# File 'lib/mocha/parameter_matchers/regexp_matches.rb', line 26

def regexp_matches(regexp)
  RegexpMatches.new(regexp)
end

- (RespondsWith) responds_with(message, result)

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

Examples:

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"

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"

See Also:



27
28
29
# File 'lib/mocha/parameter_matchers/responds_with.rb', line 27

def responds_with(message, result)
  RespondsWith.new(message, result)
end

- (YamlEquivalent) yaml_equivalent(object)

Matches any YAML that represents the specified object

Examples:

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

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

See Also:



26
27
28
# File 'lib/mocha/parameter_matchers/yaml_equivalent.rb', line 26

def yaml_equivalent(object)
  YamlEquivalent.new(object)
end