summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Foord <michael@voidspace.org.uk>2012-04-13 16:57:52 +0100
committerMichael Foord <michael@voidspace.org.uk>2012-04-13 16:57:52 +0100
commitecbd9dabe4f4311052e9b15500fa6043a02d24ec (patch)
tree41ac98c799334f5a1f2c6d9bec984c16f7d8745b
parent7e6dd774aec284894407cad2c9c620a063b5f832 (diff)
downloadmock-ecbd9dabe4f4311052e9b15500fa6043a02d24ec.tar.gz
PropertyMock attributes and return values are standard MagicMocks
-rw-r--r--docs/mock.txt13
-rw-r--r--mock.py3
-rw-r--r--tests/testhelpers.py11
3 files changed, 27 insertions, 0 deletions
diff --git a/docs/mock.txt b/docs/mock.txt
index 432f5a9..e5dcd60 100644
--- a/docs/mock.txt
+++ b/docs/mock.txt
@@ -602,6 +602,19 @@ have to create a dictionary and unpack it using `**`:
>>> mock_foo.mock_calls
[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:
+
+.. doctest::
+
+ >>> m = MagicMock()
+ >>> p = PropertyMock(return_value=3)
+ >>> type(m).foo = p
+ >>> m.foo
+ 3
+ >>> p.assert_called_once_with()
+
.. index:: __call__
.. index:: calling
diff --git a/mock.py b/mock.py
index 6ae565b..cf38238 100644
--- a/mock.py
+++ b/mock.py
@@ -2304,6 +2304,9 @@ class PropertyMock(Mock):
Fetching a `PropertyMock` instance from an object calls the mock, with
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):
return self()
def __set__(self, obj, val):
diff --git a/tests/testhelpers.py b/tests/testhelpers.py
index e80321d..b92f511 100644
--- a/tests/testhelpers.py
+++ b/tests/testhelpers.py
@@ -881,5 +881,16 @@ class TestCallList(unittest2.TestCase):
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__':
unittest2.main()