From dd13d97f602ebb08b0357701a6b22a7441c4abea Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Tue, 4 Dec 2007 23:47:03 -0200 Subject: MockerTestCase now verifies if the mocker is put in replay mode in cases where events were recorded. --- NEWS | 3 +++ mocker.py | 8 +++++++- test.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 85ed71e..bf98e44 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,9 @@ 0.X.X (2007-11-XX) ================== +- MockerTestCase now verifies if the mocker is put in replay + mode in cases where events were recorded. + - Implemented support for Deferred results as understood by Twisted Trial's TestCase, so that coexistence by multiple inheritance is possible and trivial. diff --git a/mocker.py b/mocker.py index 79f7191..e146ae4 100644 --- a/mocker.py +++ b/mocker.py @@ -98,7 +98,13 @@ class MockerTestCase(unittest.TestCase): if test_method is not None: def test_method_wrapper(): try: - result = test_method() + try: + result = test_method() + finally: + if (self.mocker.is_recording() and + self.mocker.get_events()): + raise RuntimeError("Mocker must be put in replay " + "mode with self.mocker.replay()") except: self.__cleanup() raise diff --git a/test.py b/test.py index a427c64..a1810fd 100755 --- a/test.py +++ b/test.py @@ -325,6 +325,26 @@ class MockerTestCaseTest(TestCase): self.assertEquals(str(e), str(expected_error)) self.assertEquals(type(e), type(expected_error)) + def test_raises_runtime_error_if_not_in_replay_mode_with_events(self): + class MyTest(MockerTestCase): + def test_method(self): + pass + + test = MyTest("test_method") + + # That's fine. + test.test_method() + + test.mocker.add_event(Event()) + + # That's not. + self.assertRaises(RuntimeError, test.test_method) + + test.mocker.replay() + + # Fine again. + test.test_method() + def test_mocker_is_verified_and_restored_after_test_method_is_run(self): calls = [] class MyEvent(Event): -- cgit v1.2.1