Permalink
Browse files

allow mocking of __call__ by directly calling a mock/expectation

  • Loading branch information...
timbertson committed Mar 5, 2011
1 parent a2b62b4 commit 5b0a3e9e9f354b57dcac02ab0e5919dcb1198fbc
Showing with 7 additions and 1 deletion.
  1. +5 −1 mocktest/mocking.py
  2. +2 −0 test/mocking_test.py
View
@@ -158,7 +158,7 @@ def __getattr__(self, name):
class GetWrapper(object):
"""
An intermediate object that calls its callback when an attribute
- is accesses (via __getattr__).
+ is accessed (via __getattr__).
Can only be used once, or it throws an error
"""
def __init__(self, callback):
@@ -170,6 +170,10 @@ def __getattr__(self, name):
self._used = True
return self._callback(name)
+ @property
+ def __call__(self):
+ return self.__getattr__('__call__')
+
class Object(object):
"""a named object"""
def __init__(self, name="unnamed object"):
View
@@ -109,8 +109,10 @@ def test_mocking_call(self):
self.assertRaises(TypeError, lambda: obj())
when(obj).__call__(2).then_return('two')
when(obj).__call__(3).then_return('three')
+ when(obj)(4).then_return('four')
assert obj(2) == 'two'
assert obj(3) == 'three'
+ assert obj(4) == 'four'
def test_mocking_special_methods_should_revert_class_heirarchies(self):
with MockTransaction:

0 comments on commit 5b0a3e9

Please sign in to comment.