]> granicus.if.org Git - python/commitdiff
Autospec functions should propagate mock calls to parent GH-11273
authorXtreak <tir.karthi@gmail.com>
Mon, 25 Feb 2019 21:46:34 +0000 (03:16 +0530)
committerChris Withers <chris@withers.org>
Mon, 25 Feb 2019 21:46:34 +0000 (21:46 +0000)
Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS.d/next/Library/2018-12-21-09-54-30.bpo-21478.5gsXtc.rst [new file with mode: 0644]

index ccbcd355ce31c103a646cbe972cb3ecefdc80417..2ccf0d82ce23bd25e712d7eb90bb91a60b3a62ff 100644 (file)
@@ -321,6 +321,14 @@ class _CallList(list):
 
 
 def _check_and_set_parent(parent, value, name, new_name):
+    # function passed to create_autospec will have mock
+    # attribute attached to which parent must be set
+    if isinstance(value, FunctionTypes):
+        try:
+            value = value.mock
+        except AttributeError:
+            pass
+
     if not _is_instance_mock(value):
         return False
     if ((value._mock_name or value._mock_new_name) or
index 04ab5227157801e67a12eba5a6f07e8313114f1d..2ad90ea81ec777a991bff0d655162f6d65664f53 100644 (file)
@@ -1830,5 +1830,18 @@ class MockTest(unittest.TestCase):
         self.assertEqual(type(call.parent().parent), _Call)
 
 
+    def test_parent_propagation_with_create_autospec(self):
+
+        def foo(a, b):
+            pass
+
+        mock = Mock()
+        mock.child = create_autospec(foo)
+        mock.child(1, 2)
+
+        self.assertRaises(TypeError, mock.child, 1)
+        self.assertEqual(mock.mock_calls, [call.child(1, 2)])
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2018-12-21-09-54-30.bpo-21478.5gsXtc.rst b/Misc/NEWS.d/next/Library/2018-12-21-09-54-30.bpo-21478.5gsXtc.rst
new file mode 100644 (file)
index 0000000..1000748
--- /dev/null
@@ -0,0 +1,2 @@
+Calls to a child function created with :func:`unittest.mock.create_autospec`
+should propagate to the parent. Patch by Karthikeyan Singaravelan.