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