isinstance and Mocking

class HelloWorld(object):
    def say_it(self):
        return 'Hello I am Hello World'

def i_call_hello_world(hw_obj):
    print 'here... check type: %s' %type(HelloWorld)
    if isinstance(hw_obj, HelloWorld):
        print hw_obj.say_it()

from mock import patch, MagicMock
import unittest

class TestInstance(unittest.TestCase):
    @patch('__main__.HelloWorld', spec=HelloWorld)
    def test_mock(self,MK):
        print type(MK)
        MK.say_it.return_value = 'I am fake'
        v = i_call_hello_world(MK)
        print v

if __name__ == '__main__':
    c = HelloWorld()
    i_call_hello_world(c)
    print isinstance(c, HelloWorld)
    unittest.main()

Вот трассировка

here... check type: <type 'type'>
Hello I am Hello World
True
<class 'mock.MagicMock'>
here... check type: <class 'mock.MagicMock'>
E
======================================================================
ERROR: test_mock (__main__.TestInstance)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1224, in patched
    return func(*args, **keywargs)
  File "t.py", line 18, in test_mock
    v = i_call_hello_world(MK)
  File "t.py", line 7, in i_call_hello_world
    if isinstance(hw_obj, HelloWorld):
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

----------------------------------------------------------------------
Ran 1 test in 0.002s

Q1. Почему возникает эта ошибка? Это 145

Скрытые возможности Python [закрыто] - 23 May 2017 12:34
  • 81
    Лучшие способы учить новичка к программе? [закрытый] - 24 November 2011 00:03
  • 60
    Как разбить список на куски одинакового размера? - 23 May 2017 11:55
  • 36
    Если поблочное тестирование является настолько большим, почему больше компаний не делает его? [закрытый] - 2 April 2009 23:30
  • 35
    Почему изучают Perl, Python, Ruby, если компания использует C++, C# или Java как язык приложения? [закрытый] - 20 May 2010 08:15
  • 35
    Как вы знаете, что тестировать при написании модульных тестов? [закрыто] - 2 January 2017 20:49
  • 35
    Стоит ли проводить модульное тестирование? [закрыто] - 10 April 2013 19:43