From 1a572f6c05cc6c7aa2b58e810684525cb8b66de7 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Sun, 20 Jun 2010 13:21:07 -0300 Subject: Unwrap bound methods on replace() and proxy(), as suggested by James Henstridge (#270782). --- NEWS | 3 +++ mocker.py | 2 ++ test.py | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/NEWS b/NEWS index 6560f17..2022549 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,9 @@ - Changed license to BSD, since the PSF license only applies to Python itself (#583335). +- Unwrap bound methods on replace() and proxy(), as suggested + by James Henstridge (#270782). + - Fixed support for Python 2.6. Mocking of iterators was broken in certain cases because, even though that's *not* documented, Python tries to use __length_hint__ in some cases. diff --git a/mocker.py b/mocker.py index 6a030e0..336e696 100644 --- a/mocker.py +++ b/mocker.py @@ -627,6 +627,8 @@ class MockerBase(object): for attr in attr_stack: object = getattr(object, attr) break + if isinstance(object, types.UnboundMethodType): + object = object.im_func if spec is True: spec = object if type is True: diff --git a/test.py b/test.py index 44f9efc..21c9d63 100755 --- a/test.py +++ b/test.py @@ -210,6 +210,13 @@ class IntegrationTest(TestCase): self.assertTrue(os.path.isfile("unexistent")) self.assertFalse(os.path.isfile("unexistent")) + def test_replace_class_method(self): + empty = self.mocker.replace("Queue.Queue.empty") + expect(empty()).result(False) + self.mocker.replay() + from Queue import Queue + self.assertEquals(Queue().empty(), False) + def test_patch_with_spec(self): class C(object): def method(self, a, b): @@ -1348,6 +1355,11 @@ class MockerTest(TestCase): mock = self.mocker.replace(original, passthrough=False) self.assertEquals(mock.__mocker_passthrough__, False) + def test_replace_with_bound_method(self): + from Queue import Queue + mock = self.mocker.replace(Queue.empty) + self.assertEquals(mock.__mocker_object__, Queue.empty.im_func) + def test_add_and_get_event(self): self.mocker.add_event(41) self.assertEquals(self.mocker.add_event(42), 42) -- cgit v1.2.1