Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
01bafdcc
Commit
01bafdcc
authored
Apr 14, 2014
by
Michael Foord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 17826. Setting an iterable side_effect on a mock created by create_autospec now works
parent
061cb3b0
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
2 deletions
+34
-2
Lib/unittest/mock.py
Lib/unittest/mock.py
+13
-2
Lib/unittest/test/testmock/testmock.py
Lib/unittest/test/testmock/testmock.py
+18
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/unittest/mock.py
View file @
01bafdcc
...
...
@@ -343,7 +343,14 @@ def _check_and_set_parent(parent, value, name, new_name):
value
.
_mock_name
=
name
return
True
# Internal class to identify if we wrapped an iterator object or not.
class
_MockIter
(
object
):
def
__init__
(
self
,
obj
):
self
.
obj
=
iter
(
obj
)
def
__iter__
(
self
):
return
self
def
__next__
(
self
):
return
next
(
self
.
obj
)
class
Base
(
object
):
_mock_return_value
=
DEFAULT
...
...
@@ -495,7 +502,11 @@ class NonCallableMock(Base):
delegated
=
self
.
_mock_delegate
if
delegated
is
None
:
return
self
.
_mock_side_effect
return
delegated
.
side_effect
sf
=
delegated
.
side_effect
if
sf
is
not
None
and
not
callable
(
sf
)
and
not
isinstance
(
sf
,
_MockIter
):
sf
=
_MockIter
(
sf
)
delegated
.
side_effect
=
sf
return
sf
def
__set_side_effect
(
self
,
value
):
value
=
_try_iter
(
value
)
...
...
Lib/unittest/test/testmock/testmock.py
View file @
01bafdcc
...
...
@@ -154,6 +154,24 @@ class MockTest(unittest.TestCase):
mock
=
Mock
(
side_effect
=
side_effect
,
return_value
=
sentinel
.
RETURN
)
self
.
assertEqual
(
mock
(),
sentinel
.
RETURN
)
def
test_autospec_side_effect
(
self
):
# Test for issue17826
results
=
[
1
,
2
,
3
]
def
effect
():
return
results
.
pop
()
def
f
():
pass
mock
=
create_autospec
(
f
)
mock
.
side_effect
=
[
1
,
2
,
3
]
self
.
assertEqual
([
mock
(),
mock
(),
mock
()],
[
1
,
2
,
3
],
"side effect not used correctly in create_autospec"
)
# Test where side effect is a callable
results
=
[
1
,
2
,
3
]
mock
=
create_autospec
(
f
)
mock
.
side_effect
=
effect
self
.
assertEqual
([
mock
(),
mock
(),
mock
()],
[
3
,
2
,
1
],
"callable side effect not used correctly"
)
@
unittest
.
skipUnless
(
'java'
in
sys
.
platform
,
'This test only applies to Jython'
)
...
...
Misc/NEWS
View file @
01bafdcc
...
...
@@ -33,6 +33,9 @@ Core and Builtins
Library
-------
- Issue #17826: setting an iterable side_effect on a mock function created by
create_autospec now works. Patch by Kushal Das.
- Issue #7776: Fix ``Host:`` header and reconnection when using
http.client.HTTPConnection.set_tunnel(). Patch by Nikolaus Rath.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment