Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | # Copyright (C) 2007-2012 Michael Foord & the mock team |
michael@0 | 2 | # E-mail: fuzzyman AT voidspace DOT org DOT uk |
michael@0 | 3 | # http://www.voidspace.org.uk/python/mock/ |
michael@0 | 4 | |
michael@0 | 5 | from tests.support import is_instance, unittest2, X, SomeClass |
michael@0 | 6 | |
michael@0 | 7 | from mock import ( |
michael@0 | 8 | Mock, MagicMock, NonCallableMagicMock, |
michael@0 | 9 | NonCallableMock, patch, create_autospec, |
michael@0 | 10 | CallableMixin |
michael@0 | 11 | ) |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | |
michael@0 | 15 | class TestCallable(unittest2.TestCase): |
michael@0 | 16 | |
michael@0 | 17 | def assertNotCallable(self, mock): |
michael@0 | 18 | self.assertTrue(is_instance(mock, NonCallableMagicMock)) |
michael@0 | 19 | self.assertFalse(is_instance(mock, CallableMixin)) |
michael@0 | 20 | |
michael@0 | 21 | |
michael@0 | 22 | def test_non_callable(self): |
michael@0 | 23 | for mock in NonCallableMagicMock(), NonCallableMock(): |
michael@0 | 24 | self.assertRaises(TypeError, mock) |
michael@0 | 25 | self.assertFalse(hasattr(mock, '__call__')) |
michael@0 | 26 | self.assertIn(mock.__class__.__name__, repr(mock)) |
michael@0 | 27 | |
michael@0 | 28 | |
michael@0 | 29 | def test_heirarchy(self): |
michael@0 | 30 | self.assertTrue(issubclass(MagicMock, Mock)) |
michael@0 | 31 | self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock)) |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | def test_attributes(self): |
michael@0 | 35 | one = NonCallableMock() |
michael@0 | 36 | self.assertTrue(issubclass(type(one.one), Mock)) |
michael@0 | 37 | |
michael@0 | 38 | two = NonCallableMagicMock() |
michael@0 | 39 | self.assertTrue(issubclass(type(two.two), MagicMock)) |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | def test_subclasses(self): |
michael@0 | 43 | class MockSub(Mock): |
michael@0 | 44 | pass |
michael@0 | 45 | |
michael@0 | 46 | one = MockSub() |
michael@0 | 47 | self.assertTrue(issubclass(type(one.one), MockSub)) |
michael@0 | 48 | |
michael@0 | 49 | class MagicSub(MagicMock): |
michael@0 | 50 | pass |
michael@0 | 51 | |
michael@0 | 52 | two = MagicSub() |
michael@0 | 53 | self.assertTrue(issubclass(type(two.two), MagicSub)) |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | def test_patch_spec(self): |
michael@0 | 57 | patcher = patch('%s.X' % __name__, spec=True) |
michael@0 | 58 | mock = patcher.start() |
michael@0 | 59 | self.addCleanup(patcher.stop) |
michael@0 | 60 | |
michael@0 | 61 | instance = mock() |
michael@0 | 62 | mock.assert_called_once_with() |
michael@0 | 63 | |
michael@0 | 64 | self.assertNotCallable(instance) |
michael@0 | 65 | self.assertRaises(TypeError, instance) |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | def test_patch_spec_set(self): |
michael@0 | 69 | patcher = patch('%s.X' % __name__, spec_set=True) |
michael@0 | 70 | mock = patcher.start() |
michael@0 | 71 | self.addCleanup(patcher.stop) |
michael@0 | 72 | |
michael@0 | 73 | instance = mock() |
michael@0 | 74 | mock.assert_called_once_with() |
michael@0 | 75 | |
michael@0 | 76 | self.assertNotCallable(instance) |
michael@0 | 77 | self.assertRaises(TypeError, instance) |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | def test_patch_spec_instance(self): |
michael@0 | 81 | patcher = patch('%s.X' % __name__, spec=X()) |
michael@0 | 82 | mock = patcher.start() |
michael@0 | 83 | self.addCleanup(patcher.stop) |
michael@0 | 84 | |
michael@0 | 85 | self.assertNotCallable(mock) |
michael@0 | 86 | self.assertRaises(TypeError, mock) |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | def test_patch_spec_set_instance(self): |
michael@0 | 90 | patcher = patch('%s.X' % __name__, spec_set=X()) |
michael@0 | 91 | mock = patcher.start() |
michael@0 | 92 | self.addCleanup(patcher.stop) |
michael@0 | 93 | |
michael@0 | 94 | self.assertNotCallable(mock) |
michael@0 | 95 | self.assertRaises(TypeError, mock) |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | def test_patch_spec_callable_class(self): |
michael@0 | 99 | class CallableX(X): |
michael@0 | 100 | def __call__(self): |
michael@0 | 101 | pass |
michael@0 | 102 | |
michael@0 | 103 | class Sub(CallableX): |
michael@0 | 104 | pass |
michael@0 | 105 | |
michael@0 | 106 | class Multi(SomeClass, Sub): |
michael@0 | 107 | pass |
michael@0 | 108 | |
michael@0 | 109 | class OldStyle: |
michael@0 | 110 | def __call__(self): |
michael@0 | 111 | pass |
michael@0 | 112 | |
michael@0 | 113 | class OldStyleSub(OldStyle): |
michael@0 | 114 | pass |
michael@0 | 115 | |
michael@0 | 116 | for arg in 'spec', 'spec_set': |
michael@0 | 117 | for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub: |
michael@0 | 118 | patcher = patch('%s.X' % __name__, **{arg: Klass}) |
michael@0 | 119 | mock = patcher.start() |
michael@0 | 120 | |
michael@0 | 121 | try: |
michael@0 | 122 | instance = mock() |
michael@0 | 123 | mock.assert_called_once_with() |
michael@0 | 124 | |
michael@0 | 125 | self.assertTrue(is_instance(instance, MagicMock)) |
michael@0 | 126 | # inherited spec |
michael@0 | 127 | self.assertRaises(AttributeError, getattr, instance, |
michael@0 | 128 | 'foobarbaz') |
michael@0 | 129 | |
michael@0 | 130 | result = instance() |
michael@0 | 131 | # instance is callable, result has no spec |
michael@0 | 132 | instance.assert_called_once_with() |
michael@0 | 133 | |
michael@0 | 134 | result(3, 2, 1) |
michael@0 | 135 | result.assert_called_once_with(3, 2, 1) |
michael@0 | 136 | result.foo(3, 2, 1) |
michael@0 | 137 | result.foo.assert_called_once_with(3, 2, 1) |
michael@0 | 138 | finally: |
michael@0 | 139 | patcher.stop() |
michael@0 | 140 | |
michael@0 | 141 | |
michael@0 | 142 | def test_create_autopsec(self): |
michael@0 | 143 | mock = create_autospec(X) |
michael@0 | 144 | instance = mock() |
michael@0 | 145 | self.assertRaises(TypeError, instance) |
michael@0 | 146 | |
michael@0 | 147 | mock = create_autospec(X()) |
michael@0 | 148 | self.assertRaises(TypeError, mock) |
michael@0 | 149 | |
michael@0 | 150 | |
michael@0 | 151 | def test_create_autospec_instance(self): |
michael@0 | 152 | mock = create_autospec(SomeClass, instance=True) |
michael@0 | 153 | |
michael@0 | 154 | self.assertRaises(TypeError, mock) |
michael@0 | 155 | mock.wibble() |
michael@0 | 156 | mock.wibble.assert_called_once_with() |
michael@0 | 157 | |
michael@0 | 158 | self.assertRaises(TypeError, mock.wibble, 'some', 'args') |