@@ -147,21 +147,17 @@ def _start_patch(self, mock_func, *args, **kwargs):
147
147
module, registering the patch to stop it later and returns the
148
148
mock object resulting from the mock call.
149
149
"""
150
+ self ._enforce_no_with_context (inspect .stack ())
150
151
p = mock_func (* args , ** kwargs )
151
152
mocked = p .start ()
152
153
self ._patches .append (p )
153
154
if hasattr (mocked , "reset_mock" ):
154
155
self ._mocks .append (mocked )
155
156
return mocked
156
157
157
- def object (self , * args , ** kwargs ):
158
- """API to mock.patch.object"""
159
- self ._enforce_no_with_context (inspect .stack ())
160
- return self ._start_patch (self .mock_module .patch .object , * args , ** kwargs )
161
-
162
158
def _enforce_no_with_context (self , stack ):
163
159
"""raises a ValueError if mocker is used in a with context"""
164
- caller = stack [1 ]
160
+ caller = stack [2 ]
165
161
frame = caller [0 ]
166
162
info = inspect .getframeinfo (frame )
167
163
code_context = " " .join (info .code_context ).strip ()
@@ -172,6 +168,10 @@ def _enforce_no_with_context(self, stack):
172
168
"https://github.com/pytest-dev/pytest-mock#note-about-usage-as-context-manager"
173
169
)
174
170
171
+ def object (self , * args , ** kwargs ):
172
+ """API to mock.patch.object"""
173
+ return self ._start_patch (self .mock_module .patch .object , * args , ** kwargs )
174
+
175
175
def multiple (self , * args , ** kwargs ):
176
176
"""API to mock.patch.multiple"""
177
177
return self ._start_patch (self .mock_module .patch .multiple , * args , ** kwargs )
0 commit comments