]> granicus.if.org Git - python/commitdiff
Fix exception when calling reset_mock on a mock created with autospec
authorMichael Foord <michael@voidspace.org.uk>
Sat, 9 Jun 2012 16:31:59 +0000 (17:31 +0100)
committerMichael Foord <michael@voidspace.org.uk>
Sat, 9 Jun 2012 16:31:59 +0000 (17:31 +0100)
Lib/unittest/mock.py
Lib/unittest/test/testmock/testhelpers.py
Lib/unittest/test/testmock/testmagicmethods.py

index 36be0fd0381e3aeb3de0a88ea6a392ef04952c33..4ae3d16139e27d4fc99765a632404ca4d289e819 100644 (file)
@@ -510,6 +510,8 @@ class NonCallableMock(Base):
         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 @@ class NonCallableMock(Base):
                 # 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
index 7a7145ecf61c7775c08fcaecd820fc73532bc263..8bfb29391d95f504d8f7e96ac40cb1356e1d4f28 100644 (file)
@@ -355,6 +355,13 @@ class SpecSignatureTest(unittest.TestCase):
         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):
index bd52e2589b91e93f81bb17371f9d56cef2eed7de..2bcf08801e4085c4df04aaeeb44f567158489244 100644 (file)
@@ -345,6 +345,14 @@ class TestMockingMagicMethods(unittest.TestCase):
         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():