summaryrefslogtreecommitdiff
path: root/docs/mocksignature.txt
blob: 651a54b35c9d45249dc6ccf546b78d0c60c47804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
mocksignature
=============

.. currentmodule:: mock

.. note::

    :ref:`auto-speccing`, added in mock 0.8, is a more advanced version of
    `mocksignature` and can be used for many of the same use cases.

A problem with using mock objects to replace real objects in your tests is that
:class:`Mock` can be *too* flexible. Your code can treat the mock objects in
any way and you have to manually check that they were called correctly. If your
code calls functions or methods with the wrong number of arguments then mocks
don't complain.

The solution to this is `mocksignature`, which creates functions with the
same signature as the original, but delegating to a mock. You can interrogate
the mock in the usual way to check it has been called with the *right*
arguments, but if it is called with the wrong number of arguments it will
raise a `TypeError` in the same way your production code would.

Another advantage is that your mocked objects are real functions, which can
be useful when your code uses
`inspect <http://docs.python.org/library/inspect.html>`_ or depends on
functions being function objects.

.. function:: mocksignature(func, mock=None, skipfirst=False)

    Create a new function with the same signature as `func` that delegates
    to `mock`. If `skipfirst` is True the first argument is skipped, useful
    for methods where `self` needs to be omitted from the new function.

    If you don't pass in a `mock` then one will be created for you.

    Functions returned by `mocksignature` have many of the same attributes
    and assert methods as a mock object.

    The mock is set as the `mock` attribute of the returned function for easy
    access.

    `mocksignature` can also be used with classes. It copies the signature of
    the `__init__` method.

    When used with callable objects (instances) it copies the signature of the
    `__call__` method.

`mocksignature` will work out if it is mocking the signature of a method on
an instance or a method on a class and do the "right thing" with the `self`
argument in both cases.

Because of a limitation in the way that arguments are collected by functions
created by `mocksignature` they are *always* passed as positional arguments
(including defaults) and not keyword arguments.


mocksignature api
-----------------

Although the objects returned by `mocksignature` api are real function objects,
they have much of the same api as the :class:`Mock` class. This includes the
assert methods:

.. doctest::

    >>> def func(a, b, c):
    ...     pass
    ...
    >>> func2 = mocksignature(func)
    >>> func2.called
    False
    >>> func2.return_value = 3
    >>> func2(1, 2, 3)
    3
    >>> func2.called
    True
    >>> func2.assert_called_once_with(1, 2, 3)
    >>> func2.assert_called_with(1, 2, 4)
    Traceback (most recent call last):
      ...
    AssertionError: Expected call: mock(1, 2, 4)
    Actual call: mock(1, 2, 3)
    >>> func2.call_count
    1
    >>> func2.side_effect = IndexError
    >>> func2(4, 5, 6)
    Traceback (most recent call last):
      ...
    IndexError

The mock object that is being delegated to is available as the `mock` attribute
of the function created by `mocksignature`.

.. doctest::

    >>> func2.mock.mock_calls
    [call(1, 2, 3), call(4, 5, 6)]

The methods and attributes available on functions returned by `mocksignature`
are:

    :meth:`~Mock.assert_any_call`, :meth:`~Mock.assert_called_once_with`,
    :meth:`~Mock.assert_called_with`, :meth:`~Mock.assert_has_calls`,
    :attr:`~Mock.call_args`, :attr:`~Mock.call_args_list`,
    :attr:`~Mock.call_count`, :attr:`~Mock.called`,
    :attr:`~Mock.method_calls`, 'mock', attr:`~Mock.mock_calls`,
    :meth:`~Mock.reset_mock`, :attr:`~Mock.return_value`, and
    :attr:`~Mock.side_effect`.


Example use
-----------

Basic use
~~~~~~~~~

.. doctest::

    >>> def function(a, b, c=None):
    ...     pass
    ...
    >>> mock = Mock()
    >>> function = mocksignature(function, mock)
    >>> function()
    Traceback (most recent call last):
      ...
    TypeError: <lambda>() takes at least 2 arguments (0 given)
    >>> function.return_value = 'some value'
    >>> function(1, 2, 'foo')
    'some value'
    >>> function.assert_called_with(1, 2, 'foo')


Keyword arguments
~~~~~~~~~~~~~~~~~

Note that arguments to functions created by `mocksignature` are always passed
in to the underlying mock by position even when called with keywords:

.. doctest::

    >>> def function(a, b, c=None):
    ...     pass
    ...
    >>> function = mocksignature(function)
    >>> function.return_value = None
    >>> function(1, 2)
    >>> function.assert_called_with(1, 2, None)


Mocking methods and self
~~~~~~~~~~~~~~~~~~~~~~~~

When you use `mocksignature` to replace a method on a class then `self`
will be included in the method signature - and you will need to include
the instance when you do your asserts.

As a curious factor of the way Python (2) wraps methods fetched from a class,
we can *get* the `return_value` from a function set on a class, but we can't
set it. We have to do this through the exposed `mock` attribute instead:

.. doctest::

    >>> class SomeClass(object):
    ...     def method(self, a, b, c=None):
    ...         pass
    ...
    >>> SomeClass.method = mocksignature(SomeClass.method)
    >>> SomeClass.method.mock.return_value = None
    >>> instance = SomeClass()
    >>> instance.method()
    Traceback (most recent call last):
      ...
    TypeError: <lambda>() takes at least 4 arguments (1 given)
    >>> instance.method(1, 2, 3)
    >>> instance.method.assert_called_with(instance, 1, 2, 3)

When you use `mocksignature` on instance methods `self` isn't included (and we
can set the `return_value` etc directly):

.. doctest::

    >>> class SomeClass(object):
    ...     def method(self, a, b, c=None):
    ...         pass
    ...
    >>> instance = SomeClass()
    >>> instance.method = mocksignature(instance.method)
    >>> instance.method.return_value = None
    >>> instance.method(1, 2, 3)
    >>> instance.method.assert_called_with(1, 2, 3)


mocksignature with classes
~~~~~~~~~~~~~~~~~~~~~~~~~~

When used with a class `mocksignature` copies the signature of the `__init__`
method.

.. doctest::

    >>> class Something(object):
    ...     def __init__(self, foo, bar):
    ...         pass
    ...
    >>> MockSomething = mocksignature(Something)
    >>> instance = MockSomething(10, 9)
    >>> assert instance is MockSomething.return_value
    >>> MockSomething.assert_called_with(10, 9)
    >>> MockSomething()
    Traceback (most recent call last):
      ...
    TypeError: <lambda>() takes at least 2 arguments (0 given)

Because the object returned by `mocksignature` is a function rather than a
`Mock` you lose the other capabilities of `Mock`, like dynamic attribute
creation.


mocksignature with callable objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When used with a callable object `mocksignature` copies the signature of the
`__call__` method.

.. doctest::

    >>> class Something(object):
    ...     def __call__(self, spam, eggs):
    ...         pass
    ...
    >>> something = Something()
    >>> mock_something = mocksignature(something)
    >>> result = mock_something(10, 9)
    >>> mock_something.assert_called_with(10, 9)
    >>> mock_something()
    Traceback (most recent call last):
      ...
    TypeError: <lambda>() takes at least 2 arguments (0 given)


mocksignature argument to patch
-------------------------------

`mocksignature` is available as a keyword argument to :func:`patch` or
:func:`patch.object`. It can be used with functions / methods / classes and
callable objects.

.. doctest::

    >>> class SomeClass(object):
    ...     def method(self, a, b, c=None):
    ...         pass
    ...
    >>> @patch.object(SomeClass, 'method', mocksignature=True)
    ... def test(mock_method):
    ...     instance = SomeClass()
    ...     mock_method.return_value = None
    ...     instance.method(1, 2)
    ...     mock_method.assert_called_with(instance, 1, 2, None)
    ...
    >>> test()