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/anything.rb,
lib/mocha/parameter_matchers/includes.rb,
lib/mocha/parameter_matchers/has_entry.rb,
lib/mocha/parameter_matchers/has_value.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/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, RegexpMatches, RespondsWith, YamlEquivalent

Instance Method Summary collapse

Instance Method Details

#all_of(*matchers) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/all_of.rb', line 19

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

#any_of(*matchers) ⇒ Object

:call-seq: 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


24
25
26
# File 'lib/mocha/parameter_matchers/any_of.rb', line 24

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

#any_parametersObject

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/any_parameters.rb', line 19

def any_parameters
  AnyParameters.new
end

#anythingObject

:call-seq: anything() -> parameter_matcher

Matches any object.

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


14
15
16
# File 'lib/mocha/parameter_matchers/anything.rb', line 14

def anything
  Anything.new
end

#equals(value) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/equals.rb', line 19

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

#has_entries(entries) ⇒ Object

:call-seq: 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


21
22
23
# File 'lib/mocha/parameter_matchers/has_entries.rb', line 21

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

#has_entry(*options) ⇒ Object

:call-seq: 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


30
31
32
33
34
# File 'lib/mocha/parameter_matchers/has_entry.rb', line 30

def has_entry(*options)
  key, value = options.shift, options.shift
  key, value = key.to_a[0][0..1] if key.is_a?(Hash)
  HasEntry.new(key, value)
end

#has_key(key) ⇒ Object

:call-seq: 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'


19
20
21
# File 'lib/mocha/parameter_matchers/has_key.rb', line 19

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

#has_value(value) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/has_value.rb', line 19

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

#includes(item) ⇒ Object

:call-seq: 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'.


17
18
19
# File 'lib/mocha/parameter_matchers/includes.rb', line 17

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

#instance_of(klass) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/instance_of.rb', line 19

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

#is_a(klass) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/is_a.rb', line 19

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

#kind_of(klass) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/kind_of.rb', line 19

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

#Not(matcher) ⇒ Object

:call-seq: 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


19
20
21
# File 'lib/mocha/parameter_matchers/not.rb', line 19

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

#optionally(*matchers) ⇒ Object

:call-seq: 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


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

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

#regexp_matches(regexp) ⇒ Object

:call-seq: 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


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

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

#responds_with(message, result) ⇒ Object

:call-seq: 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"


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

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

#yaml_equivalent(object) ⇒ Object

:call-seq: 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


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

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