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

僅模擬對(duì)象上的單個(gè)方法

Mocking only a single method on an object(僅模擬對(duì)象上的單個(gè)方法)
本文介紹了僅模擬對(duì)象上的單個(gè)方法的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我熟悉其他語言中的其他模擬庫(kù),例如 Java 中的 Mockito,但 Python 的 mock 庫(kù)讓我感到困惑.

我想測(cè)試以下課程.

類 MyClassUnderTest(object):def 子方法(自我,*args):do_dangerous_things()def main_method(self):self.submethod("什么都沒有.")

在我的測(cè)試中,我想確保在執(zhí)行 main_method 時(shí)調(diào)用了 submethod 并且使用正確的參數(shù)調(diào)用它.我不希望 submethod 運(yùn)行,因?yàn)樗鼤?huì)做危險(xiǎn)的事情.

我完全不確定如何開始使用它.Mock 的文檔非常難以理解,我不確定要模擬什么或如何模擬它.

我怎樣才能模擬 submethod 函數(shù),同時(shí)保留 main_method 中的功能?

解決方案

我想你要找的是 mock.patch.object

用 mock.patch.object(MyClassUnderTest, "submethod") 作為 submethod_mocked:submethod_mocked.return_value = 13MyClassUnderTest().main_method()submethod_mocked.assert_call_once_with(user_id, 100, self.context,self.account_type)

這里是小描述

 patch.object(目標(biāo), 屬性, new=DEFAULT,規(guī)格=無,創(chuàng)建=假,規(guī)格集=無,autospec=None, new_callable=None, **kwargs)

<塊引用>

使用模擬對(duì)象修補(bǔ)對(duì)象(目標(biāo))上的命名成員(屬性).

I'm familiar with other mocking libraries in other languages such as Mockito in Java, but Python's mock library confuses the life out of me.

I have the following class which I would like to test.

class MyClassUnderTest(object):

    def submethod(self, *args):
       do_dangerous_things()

    def main_method(self):
       self.submethod("Nothing.")

In my tests, I'd like to make sure that the submethod was called when main_method was executed and that it was called with the right arguments. I don't want submethod to run, as it does dangerous things.

I'm entirely unsure as to how to get started with this. Mock's documentation is incredibly hard to understand and I'm not sure what to even mock or how to mock it.

How can I mock the submethod function, while leaving the functionality in main_method alone?

解決方案

I think what you are looking for is mock.patch.object

with mock.patch.object(MyClassUnderTest, "submethod") as submethod_mocked:
    submethod_mocked.return_value = 13
    MyClassUnderTest().main_method()
    submethod_mocked.assert_called_once_with(user_id, 100, self.context,
                                             self.account_type)

Here is small description

 patch.object(target, attribute, new=DEFAULT, 
              spec=None, create=False, spec_set=None, 
              autospec=None, new_callable=None, **kwargs)

patch the named member (attribute) on an object (target) with a mock object.

這篇關(guān)于僅模擬對(duì)象上的單個(gè)方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?)
Patch __call__ of a function(修補(bǔ)函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對(duì)象的模擬方法中調(diào)用 self?)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 黄色亚洲 | 免费在线看黄 | 亚洲精品一二三区 | 欧美bondage紧缚视频 | 精品日本久久久久久久久久 | 中文字幕视频在线观看 | 亚洲欧美日韩成人在线 | 操久久 | 亚洲一区中文字幕 | 亚洲精品视频免费观看 | 福利视频1000 | 最新中文字幕久久 | 天天拍天天操 | 色综合一区二区三区 | 亚洲免费成人av | 国产1区在线 | 亚洲成人精品影院 | 日韩在线视频一区 | 精品乱码一区二区 | 日本高清不卡视频 | 国产h在线 | 日韩久久综合网 | 精品乱码一区二区三四区 | 福利视频大全 | 成人黄色在线视频 | 欧美日韩精品国产 | 欧美日韩高清一区 | 欧美黑人一区 | 日本天天操 | 黄片毛片在线观看 | 久久精品成人 | 成人免费在线播放视频 | 日韩网站在线 | 亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区 | 国产日韩一区二区三免费高清 | 日日夜精品视频 | 国产美女黄色片 | 国产成人精品一区 | 91精品国产综合久久久久久蜜臀 | 中文久久| 亚州精品天堂中文字幕 |