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
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
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
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
Matches any object.
object = mock()
object.expects(:method_1).with(anything)
object.method_1('foo')
# no error raised
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
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
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
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'
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
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'.
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
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
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
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
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
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"
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