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
c287062f
Commit
c287062f
authored
Apr 13, 2012
by
Michael Foord
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unittest.mock.PropertyMock return value and attributes are now standard MagicMocks
parent
633b32a7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
0 deletions
+25
-0
Doc/library/unittest.mock.rst
Doc/library/unittest.mock.rst
+11
-0
Lib/unittest/mock.py
Lib/unittest/mock.py
+3
-0
Lib/unittest/test/testmock/testhelpers.py
Lib/unittest/test/testmock/testhelpers.py
+11
-0
No files found.
Doc/library/unittest.mock.rst
View file @
c287062f
...
@@ -712,6 +712,17 @@ have to create a dictionary and unpack it using `**`:
...
@@ -712,6 +712,17 @@ have to create a dictionary and unpack it using `**`:
>>> mock_foo.mock_calls
>>> mock_foo.mock_calls
[call(), call(6)]
[call(), call(6)]
Because of the way mock attributes are stored you can't directly attach a
`PropertyMock` to a mock object. Instead you can attach it to the mock type
object::
>>> m = MagicMock()
>>> p = PropertyMock(return_value=3)
>>> type(m).foo = p
>>> m.foo
3
>>> p.assert_called_once_with()
Calling
Calling
~~~~~~~
~~~~~~~
...
...
Lib/unittest/mock.py
View file @
c287062f
...
@@ -2166,6 +2166,9 @@ class PropertyMock(Mock):
...
@@ -2166,6 +2166,9 @@ class PropertyMock(Mock):
Fetching a `PropertyMock` instance from an object calls the mock, with
Fetching a `PropertyMock` instance from an object calls the mock, with
no args. Setting it calls the mock with the value being set.
no args. Setting it calls the mock with the value being set.
"""
"""
def
_get_child_mock
(
self
,
**
kwargs
):
return
MagicMock
(
**
kwargs
)
def
__get__
(
self
,
obj
,
obj_type
):
def
__get__
(
self
,
obj
,
obj_type
):
return
self
()
return
self
()
def
__set__
(
self
,
obj
,
val
):
def
__set__
(
self
,
obj
,
val
):
...
...
Lib/unittest/test/testmock/testhelpers.py
View file @
c287062f
...
@@ -831,5 +831,16 @@ class TestCallList(unittest.TestCase):
...
@@ -831,5 +831,16 @@ class TestCallList(unittest.TestCase):
p
.
stop
()
p
.
stop
()
def
test_propertymock_returnvalue
(
self
):
m
=
MagicMock
()
p
=
PropertyMock
()
type
(
m
).
foo
=
p
returned
=
m
.
foo
p
.
assert_called_once_with
()
self
.
assertIsInstance
(
returned
,
MagicMock
)
self
.
assertNotIsInstance
(
returned
,
PropertyMock
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
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