bpo-38932: Mock fully resets child objects on reset_mock(). (GH-17409)

This commit is contained in:
Vegard Stikbakke 2020-01-25 16:44:46 +01:00 committed by Chris Withers
parent 9bfb4a7061
commit aef7dc8987
3 changed files with 15 additions and 2 deletions

View File

@ -592,7 +592,7 @@ class NonCallableMock(Base):
for child in self._mock_children.values(): for child in self._mock_children.values():
if isinstance(child, _SpecState) or child is _deleted: if isinstance(child, _SpecState) or child is _deleted:
continue continue
child.reset_mock(visited) child.reset_mock(visited, return_value=return_value, side_effect=side_effect)
ret = self._mock_return_value ret = self._mock_return_value
if _is_instance_mock(ret) and ret is not self: if _is_instance_mock(ret) and ret is not self:

View File

@ -1636,11 +1636,23 @@ class MockTest(unittest.TestCase):
self.assertNotEqual(m.side_effect, None) self.assertNotEqual(m.side_effect, None)
def test_reset_sideeffect(self): def test_reset_sideeffect(self):
m = Mock(return_value=10, side_effect=[2,3]) m = Mock(return_value=10, side_effect=[2, 3])
m.reset_mock(side_effect=True) m.reset_mock(side_effect=True)
self.assertEqual(m.return_value, 10) self.assertEqual(m.return_value, 10)
self.assertEqual(m.side_effect, None) self.assertEqual(m.side_effect, None)
def test_reset_return_with_children(self):
m = MagicMock(f=MagicMock(return_value=1))
self.assertEqual(m.f(), 1)
m.reset_mock(return_value=True)
self.assertNotEqual(m.f(), 1)
def test_reset_return_with_children_side_effect(self):
m = MagicMock(f=MagicMock(side_effect=[2, 3]))
self.assertNotEqual(m.f.side_effect, None)
m.reset_mock(side_effect=True)
self.assertEqual(m.f.side_effect, None)
def test_mock_add_spec(self): def test_mock_add_spec(self):
class _One(object): class _One(object):
one = 1 one = 1

View File

@ -0,0 +1 @@
Mock fully resets child objects on reset_mock(). Patch by Vegard Stikbakke