summaryrefslogtreecommitdiff
path: root/mocker.py
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2010-07-29 20:51:24 -0400
committerGustavo Niemeyer <gustavo@niemeyer.net>2010-07-29 20:51:24 -0400
commitc073b8161478bd8068dbdc9780733104a5346bcb (patch)
tree92f6ce457038fb64f8644d037fb7c3c50dc0ceee /mocker.py
parent3a1d9b8548f074c44c853e0ccaeb40edf83a7b0b (diff)
downloadmocker-c073b8161478bd8068dbdc9780733104a5346bcb.tar.gz
mocker.call() now supports a with_object argument. If True, the called
function will receive the patched or proxied object so that its state may be used or verified in checks.
Diffstat (limited to 'mocker.py')
-rw-r--r--mocker.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/mocker.py b/mocker.py
index 2d7f59b..a453ab5 100644
--- a/mocker.py
+++ b/mocker.py
@@ -847,14 +847,17 @@ class MockerBase(object):
raise exception
self.call(raise_exception)
- def call(self, func):
+ def call(self, func, with_object=False):
"""Make the last recorded event cause the given function to be called.
@param func: Function to be called.
The result of the function will be used as the event result.
"""
- self._events[-1].add_task(FunctionRunner(func))
+ event = self._events[-1]
+ if with_object and event.path.root_object is None:
+ raise TypeError("Mock object isn't a proxy")
+ event.add_task(FunctionRunner(func, with_root_object=with_object))
def count(self, min, max=False):
"""Last recorded event must be replayed between min and max times.
@@ -1845,12 +1848,16 @@ class FunctionRunner(Task):
and the function result is also returned.
"""
- def __init__(self, func):
+ def __init__(self, func, with_root_object=False):
self._func = func
+ self._with_root_object = with_root_object
def run(self, path):
action = path.actions[-1]
- return self._func(*action.args, **action.kwargs)
+ if self._with_root_object:
+ return self._func(path.root_object, *action.args, **action.kwargs)
+ else:
+ return self._func(*action.args, **action.kwargs)
class PathExecuter(Task):