testcallable.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. import unittest
  5. from unittest.test.testmock.support import is_instance, X, SomeClass
  6. from unittest.mock import (
  7. Mock, MagicMock, NonCallableMagicMock,
  8. NonCallableMock, patch, create_autospec,
  9. CallableMixin
  10. )
  11. class TestCallable(unittest.TestCase):
  12. def assertNotCallable(self, mock):
  13. self.assertTrue(is_instance(mock, NonCallableMagicMock))
  14. self.assertFalse(is_instance(mock, CallableMixin))
  15. def test_non_callable(self):
  16. for mock in NonCallableMagicMock(), NonCallableMock():
  17. self.assertRaises(TypeError, mock)
  18. self.assertFalse(hasattr(mock, '__call__'))
  19. self.assertIn(mock.__class__.__name__, repr(mock))
  20. def test_hierarchy(self):
  21. self.assertTrue(issubclass(MagicMock, Mock))
  22. self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock))
  23. def test_attributes(self):
  24. one = NonCallableMock()
  25. self.assertTrue(issubclass(type(one.one), Mock))
  26. two = NonCallableMagicMock()
  27. self.assertTrue(issubclass(type(two.two), MagicMock))
  28. def test_subclasses(self):
  29. class MockSub(Mock):
  30. pass
  31. one = MockSub()
  32. self.assertTrue(issubclass(type(one.one), MockSub))
  33. class MagicSub(MagicMock):
  34. pass
  35. two = MagicSub()
  36. self.assertTrue(issubclass(type(two.two), MagicSub))
  37. def test_patch_spec(self):
  38. patcher = patch('%s.X' % __name__, spec=True)
  39. mock = patcher.start()
  40. self.addCleanup(patcher.stop)
  41. instance = mock()
  42. mock.assert_called_once_with()
  43. self.assertNotCallable(instance)
  44. self.assertRaises(TypeError, instance)
  45. def test_patch_spec_set(self):
  46. patcher = patch('%s.X' % __name__, spec_set=True)
  47. mock = patcher.start()
  48. self.addCleanup(patcher.stop)
  49. instance = mock()
  50. mock.assert_called_once_with()
  51. self.assertNotCallable(instance)
  52. self.assertRaises(TypeError, instance)
  53. def test_patch_spec_instance(self):
  54. patcher = patch('%s.X' % __name__, spec=X())
  55. mock = patcher.start()
  56. self.addCleanup(patcher.stop)
  57. self.assertNotCallable(mock)
  58. self.assertRaises(TypeError, mock)
  59. def test_patch_spec_set_instance(self):
  60. patcher = patch('%s.X' % __name__, spec_set=X())
  61. mock = patcher.start()
  62. self.addCleanup(patcher.stop)
  63. self.assertNotCallable(mock)
  64. self.assertRaises(TypeError, mock)
  65. def test_patch_spec_callable_class(self):
  66. class CallableX(X):
  67. def __call__(self): pass
  68. class Sub(CallableX):
  69. pass
  70. class Multi(SomeClass, Sub):
  71. pass
  72. for arg in 'spec', 'spec_set':
  73. for Klass in CallableX, Sub, Multi:
  74. with patch('%s.X' % __name__, **{arg: Klass}) as mock:
  75. instance = mock()
  76. mock.assert_called_once_with()
  77. self.assertTrue(is_instance(instance, MagicMock))
  78. # inherited spec
  79. self.assertRaises(AttributeError, getattr, instance,
  80. 'foobarbaz')
  81. result = instance()
  82. # instance is callable, result has no spec
  83. instance.assert_called_once_with()
  84. result(3, 2, 1)
  85. result.assert_called_once_with(3, 2, 1)
  86. result.foo(3, 2, 1)
  87. result.foo.assert_called_once_with(3, 2, 1)
  88. def test_create_autospec(self):
  89. mock = create_autospec(X)
  90. instance = mock()
  91. self.assertRaises(TypeError, instance)
  92. mock = create_autospec(X())
  93. self.assertRaises(TypeError, mock)
  94. def test_create_autospec_instance(self):
  95. mock = create_autospec(SomeClass, instance=True)
  96. self.assertRaises(TypeError, mock)
  97. mock.wibble()
  98. mock.wibble.assert_called_once_with()
  99. self.assertRaises(TypeError, mock.wibble, 'some', 'args')
  100. if __name__ == "__main__":
  101. unittest.main()