Skip to content

Commit bb4be32

Browse files
committed
Improve tests by checking returned "spy" object
Check that the same interface applies when using the returned "spy" object from mocker.spy as it applies for using the mocked object directly
1 parent 270e975 commit bb4be32

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

pytest_mock.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ def spy(self, obj, method_name):
5252
can_autospec = True
5353

5454
if can_autospec:
55-
kwargs = dict(autospec=True)
55+
kwargs = dict(
56+
autospec=True,
57+
)
5658
else:
5759
# Can't use autospec because of https://bugs.python.org/issue23078
5860
kwargs = dict(
5961
new_callable=mock_module.MagicMock,
6062
spec=True,
6163
)
6264

63-
def func(*args, **kwargs):
64-
return method(*args, **kwargs)
65-
66-
return self.patch.object(obj, method_name, side_effect=func, **kwargs)
65+
result = self.patch.object(obj, method_name, side_effect=method, **kwargs)
66+
return result
6767

6868
def stub(self):
6969
"""
@@ -97,7 +97,6 @@ def object(self, *args, **kwargs):
9797
"""API to mock.patch.object"""
9898
return self._start_patch(mock_module.patch.object, *args, **kwargs)
9999

100-
101100
def multiple(self, *args, **kwargs):
102101
"""API to mock.patch.multiple"""
103102
return self._start_patch(mock_module.patch.multiple, *args,
@@ -107,7 +106,6 @@ def dict(self, *args, **kwargs):
107106
"""API to mock.patch.dict"""
108107
return self._start_patch(mock_module.patch.dict, *args, **kwargs)
109108

110-
111109
def __call__(self, *args, **kwargs):
112110
"""API to mock.patch"""
113111
return self._start_patch(mock_module.patch, *args, **kwargs)

test_pytest_mock.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,29 @@ def bar(self, arg):
155155
return arg * 2
156156

157157
foo = Foo()
158-
mocker.spy(foo, 'bar')
158+
other = Foo()
159+
spy = mocker.spy(foo, 'bar')
159160
assert foo.bar(arg=10) == 20
161+
assert other.bar(arg=10) == 20
160162
foo.bar.assert_called_once_with(arg=10)
163+
spy.assert_called_once_with(arg=10)
161164

162165

163166
def test_instance_method_by_class_spy(mocker):
167+
from pytest_mock import mock_module
168+
164169
class Foo(object):
165170

166171
def bar(self, arg):
167172
return arg * 2
168173

169-
mocker.spy(Foo, 'bar')
174+
spy = mocker.spy(Foo, 'bar')
170175
foo = Foo()
176+
other = Foo()
171177
assert foo.bar(arg=10) == 20
172-
foo.bar.assert_called_once_with(foo, arg=10)
178+
assert other.bar(arg=10) == 20
179+
calls = [mock_module.call(foo, arg=10), mock_module.call(other, arg=10)]
180+
assert spy.call_args_list == calls
173181

174182

175183
def test_class_method_spy(mocker):
@@ -179,9 +187,10 @@ class Foo(object):
179187
def bar(cls, arg):
180188
return arg * 2
181189

182-
mocker.spy(Foo, 'bar')
190+
spy = mocker.spy(Foo, 'bar')
183191
assert Foo.bar(arg=10) == 20
184192
Foo.bar.assert_called_once_with(arg=10)
193+
spy.assert_called_once_with(arg=10)
185194

186195

187196
def test_class_method_with_metaclass_spy(mocker):
@@ -196,10 +205,10 @@ class Foo(object):
196205
def bar(cls, arg):
197206
return arg * 2
198207

199-
mocker.spy(Foo, 'bar')
208+
spy = mocker.spy(Foo, 'bar')
200209
assert Foo.bar(arg=10) == 20
201210
Foo.bar.assert_called_once_with(arg=10)
202-
return Foo, False
211+
spy.assert_called_once_with(arg=10)
203212

204213

205214
def test_static_method_spy(mocker):
@@ -209,6 +218,7 @@ class Foo(object):
209218
def bar(arg):
210219
return arg * 2
211220

212-
mocker.spy(Foo, 'bar')
221+
spy = mocker.spy(Foo, 'bar')
213222
assert Foo.bar(arg=10) == 20
214223
Foo.bar.assert_called_once_with(arg=10)
224+
spy.assert_called_once_with(arg=10)

0 commit comments

Comments
 (0)