]> granicus.if.org Git - python/commitdiff
unittest.mock.PropertyMock return value and attributes are now standard MagicMocks
authorMichael Foord <michael@voidspace.org.uk>
Fri, 13 Apr 2012 15:57:22 +0000 (16:57 +0100)
committerMichael Foord <michael@voidspace.org.uk>
Fri, 13 Apr 2012 15:57:22 +0000 (16:57 +0100)
Doc/library/unittest.mock.rst
Lib/unittest/mock.py
Lib/unittest/test/testmock/testhelpers.py

index df1c41fae8c7526bac21006cc5c47ff5220dbcf9..f00d29f028e8b11efcd3f74d1178d19014a27815 100644 (file)
@@ -712,6 +712,17 @@ 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::
+
+    >>> m = MagicMock()
+    >>> p = PropertyMock(return_value=3)
+    >>> type(m).foo = p
+    >>> m.foo
+    3
+    >>> p.assert_called_once_with()
+
 
 Calling
 ~~~~~~~
index 0a9aece8458ffe1c9241bcccf9e6247f575bb52b..ec175426694836a0e36de12d2c90a47c8470afe0 100644 (file)
@@ -2166,6 +2166,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):
index 3674778c6d603cbbd80a5442bcaac12ec701d054..a2ed1003071fe7b5bf828a6dc3b23b9950416e3a 100644 (file)
@@ -831,5 +831,16 @@ class TestCallList(unittest.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__':
     unittest.main()