| # Copyright 2015 The Chromium Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import sys |
| import unittest |
| |
| from testing_support import auto_stub |
| |
| |
| class MockedObject(auto_stub.SimpleMock): |
| def method1(self, *args, **kwargs): |
| self._register_call(*args, **kwargs) |
| |
| |
| class TestSimpleMock(unittest.TestCase): |
| def test_auto_mock(self): |
| obj = MockedObject(self) |
| obj.method1(1, param=2) |
| obj.check_calls(['method1(1, param=2)']) |
| |
| def test_auto_mock_sorted(self): |
| obj = MockedObject(self) |
| # Regardless of order here, ... |
| obj.method1(1, c=4, a=2, b=3) |
| obj.method1(1, a=2, c=4, b=3) |
| # ... order here is sorted and hence totally deterministic. |
| obj.check_calls([ |
| 'method1(1, a=2, b=3, c=4)', |
| auto_stub.format_call('method1', 1, b=3, c=4, a=2), |
| ]) |
| |
| |
| def return_one(): |
| return 1 |
| |
| def return_two(): |
| return 2 |
| |
| class TestAutoStubTestCase(auto_stub.TestCase): |
| def test_mock_method(self): |
| # mock one function |
| self.assertEqual(return_one(), 1) |
| self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked') |
| self.assertEqual(return_one(), 'mocked') |
| auto_stub.TestCase.tearDown(self) |
| self.assertEqual(return_one(), 1) |
| |
| # mock two functions |
| self.assertEqual(return_one(), 1) |
| self.assertEqual(return_two(), 2) |
| self.mock(sys.modules[__name__], 'return_one', lambda: 'mocked1') |
| self.mock(sys.modules[__name__], 'return_two', lambda: 'mocked2') |
| self.assertEqual(return_one(), 'mocked1') |
| self.assertEqual(return_two(), 'mocked2') |
| auto_stub.TestCase.tearDown(self) |
| self.assertEqual(return_one(), 1) |
| self.assertEqual(return_two(), 2) |