OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import sys |
| 6 import unittest |
| 7 |
| 8 from testing_support import auto_stub |
| 9 |
| 10 |
| 11 class MockedObject(auto_stub.SimpleMock): |
| 12 def method1(self, *args, **kwargs): |
| 13 self._register_call(*args, **kwargs) |
| 14 |
| 15 |
| 16 class TestSimpleMock(unittest.TestCase): |
| 17 def test_auto_mock(self): |
| 18 obj = MockedObject(self) |
| 19 obj.method1(1, param=2) |
| 20 obj.check_calls(['method1(1, param=2)']) |
| 21 |
| 22 def return_one(): |
| 23 return 1 |
| 24 |
| 25 def return_two(): |
| 26 return 2 |
| 27 |
| 28 class TestAutoStubTestCase(auto_stub.TestCase): |
| 29 def test_mock_method(self): |
| 30 # mock one function |
| 31 self.assertEqual(return_one(), 1) |
| 32 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked') |
| 33 self.assertEqual(return_one(), 'mocked') |
| 34 auto_stub.TestCase.tearDown(self) |
| 35 self.assertEqual(return_one(), 1) |
| 36 |
| 37 # mock two functions |
| 38 self.assertEqual(return_one(), 1) |
| 39 self.assertEqual(return_two(), 2) |
| 40 self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked1') |
| 41 self.mock(sys.modules[__name__], 'return_two', lambda: 'mocked2') |
| 42 self.assertEqual(return_one(), 'mocked1') |
| 43 self.assertEqual(return_two(), 'mocked2') |
| 44 auto_stub.TestCase.tearDown(self) |
| 45 self.assertEqual(return_one(), 1) |
| 46 self.assertEqual(return_two(), 2) |
OLD | NEW |