From c073b8161478bd8068dbdc9780733104a5346bcb Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Thu, 29 Jul 2010 20:51:24 -0400 Subject: 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. --- mocker.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'mocker.py') 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): -- cgit v1.2.1