diff options
author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2010-07-29 20:51:24 -0400 |
---|---|---|
committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2010-07-29 20:51:24 -0400 |
commit | c073b8161478bd8068dbdc9780733104a5346bcb (patch) | |
tree | 92f6ce457038fb64f8644d037fb7c3c50dc0ceee /test.py | |
parent | 3a1d9b8548f074c44c853e0ccaeb40edf83a7b0b (diff) | |
download | mocker-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 'test.py')
-rwxr-xr-x | test.py | 62 |
1 files changed, 62 insertions, 0 deletions
@@ -150,6 +150,26 @@ class IntegrationTest(TestCase): self.assertEquals(obj.x(24), 42) self.assertEquals(calls, [24]) + def test_call_with_object(self): + class C(object): + def method(self, a, b, c): + return "Running real method?" + + calls = [] + def func(*args, **kwargs): + calls.append(args) + calls.append(kwargs) + return 4 + + mock = self.mocker.patch(C) + mock.method(1, 2, c=3) + self.mocker.call(func, with_object=True) + self.mocker.replay() + + obj = C() + self.assertEquals(obj.method(1, 2, c=3), 4) # Mocked. + self.assertEquals(calls, [(obj, 1, 2), {"c": 3}]) + def test_generate(self): obj = self.mocker.mock() obj.x(24) @@ -1598,6 +1618,41 @@ class MockerTest(TestCase): self.mocker.call(lambda *args, **kwargs: 123) self.assertEquals(event2.run(self.path), 123) + def test_call_with_object(self): + obj = object() + mock = self.mocker.proxy(obj) + action = Action("getattr", ("attr",), {}, Path(mock, root_object=456)) + path = action.path + self.action + event1 = self.mocker.add_event(Event(path)) + event2 = self.mocker.add_event(Event(path)) + self.mocker.call(lambda *args, **kwargs: (args, kwargs), + with_object=True) + self.assertEquals(event1.get_tasks(), []) + (task,) = event2.get_tasks() + self.assertEquals(type(task), FunctionRunner) + self.assertEquals(event2.run(event2.path), ((456, "attr"), {})) + + def test_call_with_object_needs_proxy(self): + obj = object() + mock = self.mocker.proxy(obj) + action = Action("getattr", ("attr",), {}, Path(mock, root_object=456)) + path = action.path + self.action + event1 = self.mocker.add_event(Event(path)) + event2 = self.mocker.add_event(Event(path)) + self.mocker.call(lambda *args, **kwargs: (args, kwargs), + with_object=True) + self.assertEquals(event1.get_tasks(), []) + (task,) = event2.get_tasks() + self.assertEquals(type(task), FunctionRunner) + self.assertEquals(event2.run(event2.path), ((456, "attr"), {})) + + def test_call_with_object_fails_on_unproxied(self): + mock = self.mocker.mock() + event1 = self.mocker.add_event(Event(Path(mock))) + event2 = self.mocker.add_event(Event(Path(mock))) + self.assertRaises(TypeError, self.mocker.call, + lambda *args: None, with_object=True) + def test_count(self): event1 = self.mocker.add_event(Event()) event2 = self.mocker.add_event(Event()) @@ -3328,6 +3383,13 @@ class FunctionRunnerTest(TestCase): result = task.run(self.path) self.assertEquals(result, "((1, 2), {'c': 3})") + def test_run_with_object(self): + task = FunctionRunner(lambda *args, **kwargs: repr((args, kwargs)), + with_root_object=True) + self.path.root_object = 4 + result = task.run(self.path) + self.assertEquals(result, "((4, 1, 2), {'c': 3})") + class PathExecuterTest(TestCase): |