Skip to content

Commit 9c3f284

Browse files
tirkarthicjw296
authored andcommitted
Autospec functions should propagate mock calls to parent GH-11273
1 parent f1b9abe commit 9c3f284

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/unittest/mock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ def __repr__(self):
321321

322322

323323
def _check_and_set_parent(parent, value, name, new_name):
324+
# function passed to create_autospec will have mock
325+
# attribute attached to which parent must be set
326+
if isinstance(value, FunctionTypes):
327+
try:
328+
value = value.mock
329+
except AttributeError:
330+
pass
331+
324332
if not _is_instance_mock(value):
325333
return False
326334
if ((value._mock_name or value._mock_new_name) or

Lib/unittest/test/testmock/testmock.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,5 +1830,18 @@ def test_parent_attribute_of_call(self):
18301830
self.assertEqual(type(call.parent().parent), _Call)
18311831

18321832

1833+
def test_parent_propagation_with_create_autospec(self):
1834+
1835+
def foo(a, b):
1836+
pass
1837+
1838+
mock = Mock()
1839+
mock.child = create_autospec(foo)
1840+
mock.child(1, 2)
1841+
1842+
self.assertRaises(TypeError, mock.child, 1)
1843+
self.assertEqual(mock.mock_calls, [call.child(1, 2)])
1844+
1845+
18331846
if __name__ == '__main__':
18341847
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Calls to a child function created with :func:`unittest.mock.create_autospec`
2+
should propagate to the parent. Patch by Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)