Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7596364

Browse files
committed
Fix exception when calling reset_mock on a mock created with autospec
1 parent afc0c77 commit 7596364

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/unittest/mock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ def reset_mock(self):
510510
self.method_calls = _CallList()
511511

512512
for child in self._mock_children.values():
513+
if isinstance(child, _SpecState):
514+
continue
513515
child.reset_mock()
514516

515517
ret = self._mock_return_value
@@ -664,6 +666,7 @@ def __setattr__(self, name, value):
664666
# but not method calls
665667
_check_and_set_parent(self, value, None, name)
666668
setattr(type(self), name, value)
669+
self._mock_children[name] = value
667670
elif name == '__class__':
668671
self._spec_class = value
669672
return

Lib/unittest/test/testmock/testhelpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,13 @@ class Foo(object):
355355
self.assertEqual(mock(), 'foo')
356356

357357

358+
def test_autospec_reset_mock(self):
359+
m = create_autospec(int)
360+
int(m)
361+
m.reset_mock()
362+
self.assertEqual(m.__int__.call_count, 0)
363+
364+
358365
def test_mocking_unbound_methods(self):
359366
class Foo(object):
360367
def foo(self, foo):

Lib/unittest/test/testmock/testmagicmethods.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ def test_magic_methods_are_magic_mocks(self):
345345
self.assertEqual(mock[1][2][3], 3)
346346

347347

348+
def test_magic_method_reset_mock(self):
349+
mock = MagicMock()
350+
str(mock)
351+
self.assertTrue(mock.__str__.called)
352+
mock.reset_mock()
353+
self.assertFalse(mock.__str__.called)
354+
355+
348356
def test_dir(self):
349357
# overriding the default implementation
350358
for mock in Mock(), MagicMock():

0 commit comments

Comments
 (0)