[Python-checkins] cpython: Fix exception when calling reset_mock on a mock created with autospec

michael.foord python-checkins at python.org
Sat Jun 9 18:32:07 CEST 2012


http://hg.python.org/cpython/rev/2059910e7d76
changeset:   77393:2059910e7d76
user:        Michael Foord <michael at voidspace.org.uk>
date:        Sat Jun 09 17:31:59 2012 +0100
summary:
  Fix exception when calling reset_mock on a mock created with autospec

files:
  Lib/unittest/mock.py                           |  3 +++
  Lib/unittest/test/testmock/testhelpers.py      |  7 +++++++
  Lib/unittest/test/testmock/testmagicmethods.py |  8 ++++++++
  3 files changed, 18 insertions(+), 0 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -510,6 +510,8 @@
         self.method_calls = _CallList()
 
         for child in self._mock_children.values():
+            if isinstance(child, _SpecState):
+                continue
             child.reset_mock()
 
         ret = self._mock_return_value
@@ -664,6 +666,7 @@
                 # but not method calls
                 _check_and_set_parent(self, value, None, name)
                 setattr(type(self), name, value)
+                self._mock_children[name] = value
         elif name == '__class__':
             self._spec_class = value
             return
diff --git a/Lib/unittest/test/testmock/testhelpers.py b/Lib/unittest/test/testmock/testhelpers.py
--- a/Lib/unittest/test/testmock/testhelpers.py
+++ b/Lib/unittest/test/testmock/testhelpers.py
@@ -355,6 +355,13 @@
         self.assertEqual(mock(), 'foo')
 
 
+    def test_autospec_reset_mock(self):
+        m = create_autospec(int)
+        int(m)
+        m.reset_mock()
+        self.assertEqual(m.__int__.call_count, 0)
+
+
     def test_mocking_unbound_methods(self):
         class Foo(object):
             def foo(self, foo):
diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py
--- a/Lib/unittest/test/testmock/testmagicmethods.py
+++ b/Lib/unittest/test/testmock/testmagicmethods.py
@@ -345,6 +345,14 @@
         self.assertEqual(mock[1][2][3], 3)
 
 
+    def test_magic_method_reset_mock(self):
+        mock = MagicMock()
+        str(mock)
+        self.assertTrue(mock.__str__.called)
+        mock.reset_mock()
+        self.assertFalse(mock.__str__.called)
+
+
     def test_dir(self):
         # overriding the default implementation
         for mock in Mock(), MagicMock():

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list