Issue 17826. Setting an iterable side_effect on a mock created by cre… · pythoncapi/cpython@01bafdc · GitHub
Skip to content

Commit 01bafdc

Browse files
committed
Issue 17826. Setting an iterable side_effect on a mock created by create_autospec now works
1 parent 061cb3b commit 01bafdc

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

Lib/unittest/mock.py

Lines changed: 13 additions & 2 deletions

Lib/unittest/test/testmock/testmock.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,24 @@ def side_effect():
154154
mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
155155
self.assertEqual(mock(), sentinel.RETURN)
156156

157+
def test_autospec_side_effect(self):
158+
# Test for issue17826
159+
results = [1, 2, 3]
160+
def effect():
161+
return results.pop()
162+
def f():
163+
pass
164+
165+
mock = create_autospec(f)
166+
mock.side_effect = [1, 2, 3]
167+
self.assertEqual([mock(), mock(), mock()], [1, 2, 3],
168+
"side effect not used correctly in create_autospec")
169+
# Test where side effect is a callable
170+
results = [1, 2, 3]
171+
mock = create_autospec(f)
172+
mock.side_effect = effect
173+
self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
174+
"callable side effect not used correctly")
157175

158176
@unittest.skipUnless('java' in sys.platform,
159177
'This test only applies to Jython')

Misc/NEWS

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)