Skip to content

Commit 8882e98

Browse files
committed
Added spy and stub to README
1 parent ec2a2bc commit 8882e98

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@ fixture:
6969
ret = [mocker.Mock(return_value=True), mocker.Mock(return_value=True)]
7070
mocker.patch('mylib.func', side_effect=ret)
7171
72+
*New in version 0.5*
73+
74+
Spy
75+
---
76+
77+
*New in version 0.6*
78+
79+
The spy acts exactly like the original method in all cases, except it allows use of `mock`
80+
features with it, like retrieving call count.
81+
82+
.. code-block:: python
83+
84+
def test_spy(mocker):
85+
class Foo(object):
86+
def bar(self):
87+
return 42
88+
89+
foo = Foo()
90+
mocker.spy(foo, 'bar')
91+
assert foo.bar() == 42
92+
assert foo.bar.call_count == 1
93+
94+
Stub
95+
----
96+
97+
*New in version 0.6*
98+
99+
The stub is a mock object that accepts any arguments and is useful to test callbacks, for instance.
100+
101+
.. code-block:: python
102+
103+
def test_stub(mocker):
104+
def foo(on_something):
105+
on_something('foo', 'bar')
106+
107+
stub = mocker.stub()
108+
109+
foo(stub)
110+
stub.assert_called_once_with('foo', 'bar')
111+
72112
Note
73113
----
74114

0 commit comments

Comments
 (0)