Skip to content
Snippets Groups Projects
Commit 4b4c915a authored by Michael Foord's avatar Michael Foord
Browse files

Adding PropertyMock

parent 8f1cda09
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ The standard library version! ...@@ -15,6 +15,7 @@ The standard library version!
* Added the `mock_open` helper function for mocking open as a context manager * Added the `mock_open` helper function for mocking open as a context manager
* `__class__` is assignable, so a mock can pass an `isinstance` check without * `__class__` is assignable, so a mock can pass an `isinstance` check without
requiring a spec requiring a spec
* Addition of `PropertyMock`
2012/02/13 Version 0.8.0 2012/02/13 Version 0.8.0
......
...@@ -26,6 +26,7 @@ __all__ = ( ...@@ -26,6 +26,7 @@ __all__ = (
'NonCallableMock', 'NonCallableMock',
'NonCallableMagicMock', 'NonCallableMagicMock',
'mock_open', 'mock_open',
'PropertyMock',
) )
...@@ -2239,3 +2240,10 @@ def mock_open(mock=None, read_data=None): ...@@ -2239,3 +2240,10 @@ def mock_open(mock=None, read_data=None):
mock.return_value = handle mock.return_value = handle
return mock return mock
class PropertyMock(Mock):
"""A Mock variant with __get__ and __set__ methods to act as a property"""
def __get__(self, obj, obj_type):
return self()
def __set__(self, obj, val):
self(val)
...@@ -6,7 +6,7 @@ from tests.support import unittest2, inPy3k ...@@ -6,7 +6,7 @@ from tests.support import unittest2, inPy3k
from mock import ( from mock import (
call, _Call, create_autospec, MagicMock, call, _Call, create_autospec, MagicMock,
Mock, ANY, _CallList, patch Mock, ANY, _CallList, patch, PropertyMock
) )
from datetime import datetime from datetime import datetime
...@@ -863,6 +863,23 @@ class TestCallList(unittest2.TestCase): ...@@ -863,6 +863,23 @@ class TestCallList(unittest2.TestCase):
self.assertEqual(str(mock.mock_calls), expected) self.assertEqual(str(mock.mock_calls), expected)
def test_propertymock(self):
p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock)
mock = p.start()
try:
SomeClass.one
mock.assert_called_once_with()
s = SomeClass()
s.one
mock.assert_called_with()
self.assertEqual(mock.mock_calls, [call(), call()])
s.one = 3
self.assertEqual(mock.mock_calls, [call(), call(), call(3)])
finally:
p.stop()
if __name__ == '__main__': if __name__ == '__main__':
unittest2.main() unittest2.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment