Files

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(parameter_matcher) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/not.rb, line 19
19:     def Not(matcher)
20:       Not.new(matcher)
21:     end
all_of(*parameter_matchers) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/all_of.rb, line 19
19:     def all_of(*matchers)
20:       AllOf.new(*matchers)
21:     end
any_of(*parameter_matchers) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/any_of.rb, line 24
24:     def any_of(*matchers)
25:       AnyOf.new(*matchers)
26:     end
any_parameters() → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/any_parameters.rb, line 19
19:     def any_parameters
20:       AnyParameters.new
21:     end
anything() → parameter_matcher click to toggle source

Matches any object.

  object = mock()
  object.expects(:method_1).with(anything)
  object.method_1('foo')
  # no error raised
    # File lib/mocha/parameter_matchers/anything.rb, line 14
14:     def anything
15:       Anything.new
16:     end
equals(value) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/equals.rb, line 19
19:     def equals(value)
20:       Equals.new(value)
21:     end
has_entries(entries) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/has_entries.rb, line 21
21:     def has_entries(entries)
22:       HasEntries.new(entries)
23:     end
has_entry(key, value) → parameter_matcher has_entry(key => value) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/has_entry.rb, line 30
30:     def has_entry(*options)
31:       key, value = options.shift, options.shift
32:       key, value = key.to_a[0][0..1] if key.is_a?(Hash)
33:       HasEntry.new(key, value)
34:     end
has_key(key) → parameter_matcher click to toggle source

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'
    # File lib/mocha/parameter_matchers/has_key.rb, line 19
19:     def has_key(key)
20:       HasKey.new(key)
21:     end
has_value(value) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/has_value.rb, line 19
19:     def has_value(value)
20:       HasValue.new(value)
21:     end
includes(item) → parameter_matcher click to toggle source

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'.
    # File lib/mocha/parameter_matchers/includes.rb, line 17
17:     def includes(item)
18:       Includes.new(item)
19:     end
instance_of(klass) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/instance_of.rb, line 19
19:     def instance_of(klass)
20:       InstanceOf.new(klass)
21:     end
is_a(klass) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/is_a.rb, line 19
19:     def is_a(klass)
20:       IsA.new(klass)
21:     end
kind_of(klass) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/kind_of.rb, line 19
19:     def kind_of(klass)
20:       KindOf.new(klass)
21:     end
optionally(*parameter_matchers) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/optionally.rb, line 27
27:     def optionally(*matchers)
28:       Optionally.new(*matchers)
29:     end
regexp_matches(regular_expression) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/regexp_matches.rb, line 20
20:     def regexp_matches(regexp)
21:       RegexpMatches.new(regexp)
22:     end
responds_with(message, result) → parameter_matcher 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.

  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"
    # File lib/mocha/parameter_matchers/responds_with.rb, line 20
20:     def responds_with(message, result)
21:       RespondsWith.new(message, result)
22:     end
yaml_equivalent(object) → parameter_matcher click to toggle source

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
    # File lib/mocha/parameter_matchers/yaml_equivalent.rb, line 20
20:     def yaml_equivalent(object)
21:       YamlEquivalent.new(object)
22:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.