summaryrefslogtreecommitdiff
path: root/Lib/unittest/test/testmock/testmock.py
diff options
context:
space:
mode:
authorKarthikeyan Singaravelan <tir.karthi@gmail.com>2020-01-24 18:44:29 +0530
committerChris Withers <chris@withers.org>2020-01-24 13:14:29 +0000
commit66b00a9d3aacf6ed49412f48743e4913104a2bb3 (patch)
tree2326feba1f39e3613e1eae1fd9c5822591c2480a /Lib/unittest/test/testmock/testmock.py
parentb8d1262e8afe7b907b4a394a191739571092acdb (diff)
downloadcpython-git-66b00a9d3aacf6ed49412f48743e4913104a2bb3.tar.gz
bpo-38473: Handle autospecced functions and methods used with attach_mock (GH-16784)
Diffstat (limited to 'Lib/unittest/test/testmock/testmock.py')
-rw-r--r--Lib/unittest/test/testmock/testmock.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 01bc479444..1030d12323 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1922,6 +1922,35 @@ class MockTest(unittest.TestCase):
self.assertEqual(mock_func.mock._extract_mock_name(), 'mock.child')
+ def test_attach_mock_patch_autospec_signature(self):
+ with mock.patch(f'{__name__}.Something.meth', autospec=True) as mocked:
+ manager = Mock()
+ manager.attach_mock(mocked, 'attach_meth')
+ obj = Something()
+ obj.meth(1, 2, 3, d=4)
+ manager.assert_has_calls([call.attach_meth(mock.ANY, 1, 2, 3, d=4)])
+ obj.meth.assert_has_calls([call(mock.ANY, 1, 2, 3, d=4)])
+ mocked.assert_has_calls([call(mock.ANY, 1, 2, 3, d=4)])
+
+ with mock.patch(f'{__name__}.something', autospec=True) as mocked:
+ manager = Mock()
+ manager.attach_mock(mocked, 'attach_func')
+ something(1)
+ manager.assert_has_calls([call.attach_func(1)])
+ something.assert_has_calls([call(1)])
+ mocked.assert_has_calls([call(1)])
+
+ with mock.patch(f'{__name__}.Something', autospec=True) as mocked:
+ manager = Mock()
+ manager.attach_mock(mocked, 'attach_obj')
+ obj = Something()
+ obj.meth(1, 2, 3, d=4)
+ manager.assert_has_calls([call.attach_obj(),
+ call.attach_obj().meth(1, 2, 3, d=4)])
+ obj.meth.assert_has_calls([call(1, 2, 3, d=4)])
+ mocked.assert_has_calls([call(), call().meth(1, 2, 3, d=4)])
+
+
def test_attribute_deletion(self):
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
NonCallableMock()):