Used as parameters for Expectation#with to restrict the parameter values which will match the expectation. Can be nested.
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 def Not(matcher) Not.new(matcher) end
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 def all_of(*matchers) AllOf.new(*matchers) end
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 def any_of(*matchers) AnyOf.new(*matchers) end
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 def any_parameters AnyParameters.new end
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 def anything Anything.new end
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 def equals(value) Equals.new(value) end
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 def has_entries(entries) HasEntries.new(entries) end
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 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
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 def has_key(key) HasKey.new(key) end
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 def has_value(value) HasValue.new(value) end
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 def includes(item) Includes.new(item) end
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 def instance_of(klass) InstanceOf.new(klass) end
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 def is_a(klass) IsA.new(klass) end
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 def kind_of(klass) KindOf.new(klass) end
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 def optionally(*matchers) Optionally.new(*matchers) end
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 def regexp_matches(regexp) RegexpMatches.new(regexp) end
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 def responds_with(message, result) RespondsWith.new(message, result) end
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 def yaml_equivalent(object) YamlEquivalent.new(object) end
Generated with the Darkfish Rdoc Generator 2.