summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--UnitTests/mocktest.py15
-rw-r--r--mock.py15
-rw-r--r--mock.txt6
-rw-r--r--mock.wpr1
4 files changed, 24 insertions, 13 deletions
diff --git a/UnitTests/mocktest.py b/UnitTests/mocktest.py
index 865533f..46914c8 100644
--- a/UnitTests/mocktest.py
+++ b/UnitTests/mocktest.py
@@ -182,7 +182,7 @@ class MockTest(TestCase):
# this should be allowed
mock.something
self.assertRaisesWithMessage(AttributeError,
- "object has no attribute 'something_else'",
+ "Mock object has no attribute 'something_else'",
lambda: mock.something_else)
@@ -198,10 +198,10 @@ class MockTest(TestCase):
mock.x
mock.y
self.assertRaisesWithMessage(AttributeError,
- "object has no attribute 'z'",
+ "Mock object has no attribute 'z'",
lambda: mock.z)
self.assertRaisesWithMessage(AttributeError,
- "object has no attribute '__something__'",
+ "Mock object has no attribute '__something__'",
lambda: mock.__something__)
testAttributes(Mock(spec=Something))
@@ -379,6 +379,15 @@ class MockTest(TestCase):
self.assertTrue(bool(instance))
+ def testMockRaisesAttributeErrorForMagicAttributes(self):
+ mock = Mock()
+ self.assertRaisesWithMessage(AttributeError, '__something__', getattr, mock, '__something__')
+
+ mock = Mock(spec=['__something__'])
+ # shouldn't raise an AttributeError
+ mock.__something__
+
+
if __name__ == '__main__':
RunTests(MockTest)
diff --git a/mock.py b/mock.py
index f9a0f67..dc29a9a 100644
--- a/mock.py
+++ b/mock.py
@@ -25,13 +25,14 @@ __version__ = '0.5.0 alpha'
DEFAULT = object()
+def _is_magic(name):
+ return '__%s__' % name[2:-2] == name
class Mock(object):
def __new__(cls, spec=None, magics=None, *args, **kwargs):
if isinstance(spec, list):
- magics = [method[2:-2] for method in spec if ('__%s__' % method[2:-2] == method
- and method[2:-2] in magic_methods)]
+ magics = [method[2:-2] for method in spec if (_is_magic(method) and method[2:-2] in magic_methods)]
elif spec is not None:
magics = [method for method in magic_methods if hasattr(spec, '__%s__' % method)]
elif isinstance(magics, basestring):
@@ -114,11 +115,11 @@ class Mock(object):
def __getattr__(self, name):
- if name == '__length_hint__':
- # an unfortunate workaround for mocking iterables
- raise AttributeError('__length_hint__')
- if self._methods is not None and name not in self._methods:
- raise AttributeError("object has no attribute '%s'" % name)
+ if self._methods is not None:
+ if name not in self._methods:
+ raise AttributeError("Mock object has no attribute '%s'" % name)
+ elif _is_magic(name):
+ raise AttributeError(name)
if name not in self._children:
self._children[name] = Mock(parent=self, name=name)
diff --git a/mock.txt b/mock.txt
index 7d3fac4..404e2b4 100644
--- a/mock.txt
+++ b/mock.txt
@@ -879,6 +879,8 @@ CHANGELOG
only container methods are available.
* Instantiating a Mock with magics / spec will actually create a MagicMock with
magic methods from the method / spec list.
+* A Mock created without a spec will not attempt to mock any magic methods / attributes
+ (it will raise an AttributeError instead).
2008/10/12 Version 0.4.0
@@ -947,6 +949,4 @@ Distribution includes unit tests.
Initial release.
-
-
-.. _Michael Foord: http://www.voidspace.org.uk/python/weblog/index.shtml \ No newline at end of file
+.. _Michael Foord: http://www.voidspace.org.uk/python/weblog/index.shtml
diff --git a/mock.wpr b/mock.wpr
index 59d1f54..888a7d1 100644
--- a/mock.wpr
+++ b/mock.wpr
@@ -11,3 +11,4 @@ proj.directory-list = [{'dirloc': loc('.'),
'recursive': 1,
'watch_for_changes': 1}]
proj.file-type = 'shared'
+testing.auto-test-file-specs = ('*test.py',)