Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. class HelloWorld(object):
  2. def say_it(self):
  3. return 'Hello I am Hello World'
  4.  
  5. def i_call_hello_world(hw_obj):
  6. print 'here... check type: %s' %type(HelloWorld)
  7. if isinstance(hw_obj, HelloWorld):
  8. print hw_obj.say_it()
  9.  
  10. from mock import patch, MagicMock
  11. import unittest
  12.  
  13. class TestInstance(unittest.TestCase):
  14. @patch('__main__.HelloWorld', spec=HelloWorld)
  15. def test_mock(self,MK):
  16. print type(MK)
  17. MK.say_it.return_value = 'I am fake'
  18. v = i_call_hello_world(MK)
  19. print v
  20.  
  21. if __name__ == '__main__':
  22. c = HelloWorld()
  23. i_call_hello_world(c)
  24. print isinstance(c, HelloWorld)
  25. unittest.main()
  26.  
  27. here... check type: <type 'type'>
  28. Hello I am Hello World
  29. True
  30. <class 'mock.MagicMock'>
  31. here... check type: <class 'mock.MagicMock'>
  32. E
  33. ======================================================================
  34. ERROR: test_mock (__main__.TestInstance)
  35. ----------------------------------------------------------------------
  36. Traceback (most recent call last):
  37. File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1224, in patched
  38. return func(*args, **keywargs)
  39. File "t.py", line 18, in test_mock
  40. v = i_call_hello_world(MK)
  41. File "t.py", line 7, in i_call_hello_world
  42. if isinstance(hw_obj, HelloWorld):
  43. TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
  44.  
  45. ----------------------------------------------------------------------
  46. Ran 1 test in 0.002s
  47.  
  48. mock = Mock(spec=3)
  49. isinstance(mock, int)
  50. True
  51.  
  52. if hasattr(hw_obj, 'say_it'):
  53. print hw_obj.say_it()
  54.  
  55. def test_mock(self,MK):
  56. print type(MK)
  57.  
  58. MK.__bases__ = (HelloWorld,)
  59.  
  60. MK.say_it.return_value = 'I am fake'
  61. v = i_call_hello_world(MK)
  62. print v
  63.  
  64. @patch('__main__.HelloWorld', spec=HelloWorld)
  65. def test_mock(self,MK):
  66.  
  67. def test_mock(self):
  68. MK = MagicMock(spec=HelloWorld)
  69. print type(MK)
  70. MK.say_it.return_value = 'I am fake'
  71. v = i_call_hello_world(MK)
  72. print v
  73.  
  74. <class 'mock.MagicMock'>
  75. here... check type: <type 'type'>
  76. I am fake
  77. None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement