久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

isinstance 和 Mocking

isinstance and Mocking(isinstance 和 Mocking)
本文介紹了isinstance 和 Mocking的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

類 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 pass isinstance() 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 国产最新精品视频 | 婷婷一级片 | 欧美日韩第一页 | 久久曰视频 | 黑人精品 | 亚洲精品乱码8久久久久久日本 | 免费不卡视频 | 欧美日韩国产在线 | 一区二区三区av夏目彩春 | 免费av在线网站 | 日韩波多野结衣 | 午夜在线小视频 | 麻豆一区二区三区精品视频 | 亚洲一区自拍 | 成人高清视频在线观看 | 日韩在线小视频 | 国产成人一区二区三区精 | 久热精品在线观看视频 | 91精品久久久久久久久 | 国产成人在线播放 | 91久久精品 | 亚洲一区二区久久久 | 成人av在线播放 | 91中文在线观看 | 午夜免费看视频 | 日日久| 一级免费在线视频 | 91精品国产91久久久 | 一区二区三区播放 | 久久久久久国产精品 | 日本粉嫩一区二区三区视频 | 91在线视频免费观看 | 91在线一区 | 亚洲精品国产电影 | 一区二区高清 | 国产精品99久久久久 | 香蕉婷婷 | 黄色av免费网站 | www.99热这里只有精品 | 在线视频成人 | 在线观看视频福利 |