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

僅模擬對象上的單個方法

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

問題描述

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

我想測試以下課程.

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

在我的測試中,我想確保在執行 main_method 時調用了 submethod 并且使用正確的參數調用它.我不希望 submethod 運行,因為它會做危險的事情.

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

我怎樣才能模擬 submethod 函數,同時保留 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(目標, 屬性, new=DEFAULT,規格=無,創建=假,規格集=無,autospec=None, new_callable=None, **kwargs)

<塊引用>

使用模擬對象修補對象(目標)上的命名成員(屬性).

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.

這篇關于僅模擬對象上的單個方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調用 self?)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 色爽 | 亚洲成人精品视频 | 久久狠狠干 | 午夜免费福利 | 日本亚洲天堂 | 国产精品无 | 国产成人在线视频 | 久久久午夜精品 | 成人毛片在线 | 成人精品在线 | 激情五月婷婷综合 | 亚洲精品成人 | 深夜福利网站 | 欧美日韩精品一区二区在线播放 | 97精品国产露脸对白 | 激情高潮到大叫狂喷水 | 成人a毛片 | 欧美视频二区 | 国产精品久久久久久久久借妻 | 国产日韩精品一区二区 | 一区二区三区在线观看视频 | 精品一区二区三区在线观看 | 亚洲福利网 | 99中文字幕| 日韩福利一区 | 中文字幕av在线 | 日韩av手机在线观看 | 91狠狠干 | 亚洲男人的天堂在线观看 | 精品亚洲一区二区三区四区五区 | 日韩精品视频一区二区三区 | 国产福利在线视频 | 999精品在线 | 五月激情久久 | 国产视频在线观看视频 | 午夜影院免费 | 日韩大片在线观看 | 亚洲综人网 | 成人在线免费观看网站 | 激情五月综合色婷婷一区二区 | 黄色在线免费看 |