diff options
author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2007-11-22 20:01:27 -0200 |
---|---|---|
committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2007-11-22 20:01:27 -0200 |
commit | 35d2aaafce5afc718e96cc78656201488c390725 (patch) | |
tree | 6848868ad0d36ad9b640afa361f362f24e1bb52a | |
parent | 16f6a2b6573857c91d7f232c5fc3c26a1479e45b (diff) | |
download | mocker-35d2aaafce5afc718e96cc78656201488c390725.tar.gz |
In recording mode, mock.__class__ will now return Mock, and not
record the action. This allows Mocker to be used in interactive
environments which inspect the result's type, such as in iPython
(reported by Alex Dante).
-rw-r--r-- | NEWS | 9 | ||||
-rw-r--r-- | mocker.py | 7 | ||||
-rwxr-xr-x | test.py | 24 |
3 files changed, 33 insertions, 7 deletions
@@ -1,3 +1,12 @@ +0.9.2 (2007-11-22) +================== + +- In recording mode, mock.__class__ will now return Mock, and not + record the action. This allows Mocker to be used in interactive + environments which inspect the result's type, such as in iPython + (reported by Alex Dante). + + 0.9.1 (2007-11-18) ================== @@ -965,9 +965,6 @@ class Mock(object): try: return self.__mocker__.act(path) except MatchError, exception: - if (self.__mocker_type__ is not None and - kind == "getattr" and args == ("__class__",)): - return self.__mocker_type__ root_mock = path.root_mock if (path.root_object is not None and root_mock.__mocker_passthrough__): @@ -986,6 +983,10 @@ class Mock(object): def __getattribute__(self, name): if name.startswith("__mocker_"): return super(Mock, self).__getattribute__(name) + if name == "__class__": + if self.__mocker__.is_recording() or self.__mocker_type__ is None: + return type(self) + return self.__mocker_type__ return self.__mocker_act__("getattr", (name,)) def __setattr__(self, name, value): @@ -2042,6 +2042,11 @@ class MockTest(unittest.TestCase): def setUp(self): self.paths = [] class StubMocker(object): + _recording = True + def is_recording(self): + return self._recording + def replay(self): + self._recording = False @staticmethod def act(path): self.paths.append(path) @@ -2079,13 +2084,24 @@ class MockTest(unittest.TestCase): C = object() self.assertEquals(Mock(self.mocker, spec=C).__mocker_spec__, C) - def test_type(self): - def raise_exception(self, path): - raise MatchError - self.StubMocker.act = raise_exception + def test_class_without_type(self): + mock = Mock(self.mocker) + self.assertEquals(mock.__class__, Mock) + self.mocker.replay() + self.assertEquals(mock.__class__, Mock) + + def test_class_with_type_when_recording(self): class C(object): pass mock = Mock(self.mocker, type=C) self.assertEquals(mock.__mocker_type__, C) + self.assertEquals(mock.__class__, Mock) + self.assertEquals(isinstance(mock, Mock), True) + + def test_class_with_type_when_replaying(self): + class C(object): pass + mock = Mock(self.mocker, type=C) + self.mocker.replay() + self.assertEquals(mock.__mocker_type__, C) self.assertEquals(mock.__class__, C) self.assertEquals(isinstance(mock, C), True) |