]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Autospec functions should propagate mock calls to parent GH-11273 (#12039)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 3 Mar 2019 17:14:44 +0000 (09:14 -0800)
committerChris Withers <chris@withers.org>
Sun, 3 Mar 2019 17:14:44 +0000 (17:14 +0000)
(cherry picked from commit 9c3f284de598550be6687964c23fd7599e53b20e)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
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 c97ea7984a6eee7fef90500ceb112c144270bd72..5b8e7441403538ca49bb17f7d63342666d888f9b 100644 (file)
@@ -320,6 +320,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 cf0ee8fbb2344f8f3b9d9003a921c49f1e16404f..447a502b57d6eb5845b50293cfc9c886016d9e18 100644 (file)
@@ -1799,5 +1799,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.