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/
5
from tests.support import is_instance, unittest2, X, SomeClass
8
Mock, MagicMock, NonCallableMagicMock,
9
NonCallableMock, patch, create_autospec,
15
class TestCallable(unittest2.TestCase):
17
def assertNotCallable(self, mock):
18
self.assertTrue(is_instance(mock, NonCallableMagicMock))
19
self.assertFalse(is_instance(mock, CallableMixin))
22
def test_non_callable(self):
23
for mock in NonCallableMagicMock(), NonCallableMock():
24
self.assertRaises(TypeError, mock)
25
self.assertFalse(hasattr(mock, '__call__'))
26
self.assertIn(mock.__class__.__name__, repr(mock))
29
def test_heirarchy(self):
30
self.assertTrue(issubclass(MagicMock, Mock))
31
self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock))
34
def test_attributes(self):
35
one = NonCallableMock()
36
self.assertTrue(issubclass(type(one.one), Mock))
38
two = NonCallableMagicMock()
39
self.assertTrue(issubclass(type(two.two), MagicMock))
42
def test_subclasses(self):
47
self.assertTrue(issubclass(type(one.one), MockSub))
49
class MagicSub(MagicMock):
53
self.assertTrue(issubclass(type(two.two), MagicSub))
56
def test_patch_spec(self):
57
patcher = patch('%s.X' % __name__, spec=True)
58
mock = patcher.start()
59
self.addCleanup(patcher.stop)
62
mock.assert_called_once_with()
64
self.assertNotCallable(instance)
65
self.assertRaises(TypeError, instance)
68
def test_patch_spec_set(self):
69
patcher = patch('%s.X' % __name__, spec_set=True)
70
mock = patcher.start()
71
self.addCleanup(patcher.stop)
74
mock.assert_called_once_with()
76
self.assertNotCallable(instance)
77
self.assertRaises(TypeError, instance)
80
def test_patch_spec_instance(self):
81
patcher = patch('%s.X' % __name__, spec=X())
82
mock = patcher.start()
83
self.addCleanup(patcher.stop)
85
self.assertNotCallable(mock)
86
self.assertRaises(TypeError, mock)
89
def test_patch_spec_set_instance(self):
90
patcher = patch('%s.X' % __name__, spec_set=X())
91
mock = patcher.start()
92
self.addCleanup(patcher.stop)
94
self.assertNotCallable(mock)
95
self.assertRaises(TypeError, mock)
98
def test_patch_spec_callable_class(self):
103
class Sub(CallableX):
106
class Multi(SomeClass, Sub):
113
class OldStyleSub(OldStyle):
116
for arg in 'spec', 'spec_set':
117
for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:
118
patcher = patch('%s.X' % __name__, **{arg: Klass})
119
mock = patcher.start()
123
mock.assert_called_once_with()
125
self.assertTrue(is_instance(instance, MagicMock))
127
self.assertRaises(AttributeError, getattr, instance,
131
# instance is callable, result has no spec
132
instance.assert_called_once_with()
135
result.assert_called_once_with(3, 2, 1)
137
result.foo.assert_called_once_with(3, 2, 1)
142
def test_create_autopsec(self):
143
mock = create_autospec(X)
145
self.assertRaises(TypeError, instance)
147
mock = create_autospec(X())
148
self.assertRaises(TypeError, mock)
151
def test_create_autospec_instance(self):
152
mock = create_autospec(SomeClass, instance=True)
154
self.assertRaises(TypeError, mock)
156
mock.wibble.assert_called_once_with()
158
self.assertRaises(TypeError, mock.wibble, 'some', 'args')