久久久久久久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 的意思不同)
主站蜘蛛池模板: 中文字幕亚洲一区 | 羞羞的视频免费在线观看 | 国产精品不卡 | 日韩成人性视频 | 久久婷婷色 | 天天操夜夜操免费视频 | 国产1页| 中文字幕精品视频 | 日韩在线视频一区二区三区 | 久久婷婷麻豆国产91天堂 | 久久蜜桃资源一区二区老牛 | 亚洲中午字幕 | 四虎海外 | 中文字幕福利 | 亚洲男女激情 | 午夜小视频在线播放 | 亚洲一区在线播放 | 在线国产视频 | 成人国产在线视频 | 在线视频日韩 | 午夜在线视频一区二区三区 | 成人二区| 欧美日本韩国一区二区三区 | 欧美福利视频 | www.色午夜.com| 91热在线 | 久久精品 | 久久精品国产一区二区电影 | 久久久久久国产免费视网址 | 自拍视频精品 | 午夜精品福利视频 | 日韩在线视频免费观看 | 天天爽天天操 | 色吧色综合 | 中文字幕第十一页 | 99久久国产免费 | 欧美成人一区二区三区 | 九九爱这里只有精品 | 久久国产高清视频 | 国产精品美女久久久久久久网站 | 国产欧美三区 |