問題描述
類 HelloWorld(object):def say_it(self):返回'你好我是你好世界'def i_call_hello_world(hw_obj):print 'here... 檢查類型:%s' %type(HelloWorld)如果是實例(hw_obj,HelloWorld):打印 hw_obj.say_it()來自模擬導入補丁,MagicMock導入單元測試類TestInstance(unittest.TestCase):@patch('__main__.HelloWorld', spec=HelloWorld)def test_mock(self,MK):印刷類型(MK)MK.say_it.return_value = '我是假的'v = i_call_hello_world(MK)打印 v如果 __name__ == '__main__':c = 你好世界()i_call_hello_world(c)打印 isinstance(c, HelloWorld)單元測試.main()
這是回溯
這里...檢查類型:<type 'type'>你好我是你好世界真的<類'mock.MagicMock'>這里...檢查類型:<class 'mock.MagicMock'>乙=========================================================================錯誤:test_mock (__main__.TestInstance)----------------------------------------------------------------------回溯(最近一次通話最后):文件/usr/local/lib/python2.7/dist-packages/mock.py",第 1224 行,已修補返回函數(*args,**keywargs)文件t.py",第 18 行,在 test_mockv = i_call_hello_world(MK)i_call_hello_world 中的文件t.py",第 7 行如果是實例(hw_obj,HelloWorld):TypeError: isinstance() arg 2 必須是類、類型或類和類型的元組----------------------------------------------------------------------在 0.002 秒內運行 1 次測試
Q1.為什么會拋出這個錯誤?它們是 <class type='MagicMock>
Q2.如果錯誤得到修復,如何暫停模擬以便第一行通過?
來自 文檔:p><塊引用>
通常,對象的 __class__
屬性將返回其類型.對于具有規范的模擬對象,__class__
改為返回規范類.這允許模擬對象通過 isinstance()
測試它們正在替換/偽裝為的對象:
mock = Mock(spec=3)isinstance(模擬,int)真的
不要使用 isinstance
,而是檢查 say_it
方法是否存在.如果方法存在,調用它:
if hasattr(hw_obj, 'say_it'):打印 hw_obj.say_it()
無論如何,這是一個更好的設計:依賴類型信息更加脆弱.
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 is the traceback
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. Why is this error thrown? They are <class type='MagicMock>
Q2. How do I pause the mocking so that the first line will pass if the error is fixed?
From the docs:
Normally the
__class__
attribute of an object will return its type. For a mock object with a spec,__class__
returns the spec class instead. This allows mock objects to passisinstance()
tests for the object they are replacing / masquerading as:
mock = Mock(spec=3)
isinstance(mock, int)
True
Don't use isinstance
, instead check for the existence of the say_it
method. If the method exists, call it:
if hasattr(hw_obj, 'say_it'):
print hw_obj.say_it()
This is a better design anyway: relying on type information is much more brittle.
這篇關于isinstance 和 Mocking的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!