mockify.matchers - Classes for wildcarding expected arguments

Module containing predefined matchers.

A matcher is every class that inherits from Matcher and implements following methods:

__repr__(self)
Return matcher’s text representation.
__eq__(self, other)

Check if self is equal to other.

Here we use standard Python __eq__ operator as it will be automatically executed by Python no matter where the matcher is used. But equality definition is completely up to the matcher implementation.

class mockify.matchers.Any

Bases: mockify.matchers.Matcher

Matcher that matches any value.

It is available also as _ (underscore) single instance that can be imported from this module.

For example, you can record expectation that mock must be called with one positional argument of any value but exactly 3 times:

>>> from mockify.matchers import _
>>> from mockify.mock import Function
>>> foo = Function('foo')
>>> foo.expect_call(_).times(3)
<mockify.Expectation: foo(_)>
>>> for i in range(3):
...     foo(i)
>>> foo.assert_satisfied()
class mockify.matchers.Matcher

Bases: object

Base class for matchers.

class mockify.matchers.SaveArg

Bases: mockify.matchers.Matcher

Matcher that matches any value and keeps ordered track of unique values.

This can be used as a replacement for Any in case that you need to ensure that mock was called in specified order.

For example:

>>> from mockify.mock import Function
>>> arg = SaveArg()
>>> foo = Function('foo')
>>> foo.expect_call(arg).times(3)
<mockify.Expectation: foo(SaveArg)>
>>> for i in range(3):
...     foo(i)
>>> foo.assert_satisfied()
>>> arg.called_with == [0, 1, 2]
True
called_with

List of ordered unique values that this matcher was called with.