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

我們如何確保 Mock.call_args_list 中的調用包含具有

How do we ensure that the calls in the Mock.call_args_list contain calls with arguments at the same state of when the Mock object was called?(我們如何確保 Mock.call_args_list 中的調用包含具有與調用 Mock 對象時相同狀態的
本文介紹了我們如何確保 Mock.call_args_list 中的調用包含具有與調用 Mock 對象時相同狀態的參數的調用?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

from mock import Mock
j = []
u = Mock()
u(j)
# At this point u.call_args_list == [call([])]
print u.call_args_list
j.append(100)
# At this point u.call_args_list == [call([100])], but I expect it to be [call([])], since it was never called when j had a value of 100 in it
print u.call_args_list

我的問題是如何確保 u.call_args_list 中的調用在調用模擬時而不是在檢查模擬參數時包含所有對象的狀態?

My question is how do I ensure that the calls in u.call_args_list contain the states of all objects at the time of calling the mock rather than at the time of checking the arguments of the mock?

我目前正在使用 mock==1.0.1.

推薦答案

這在文檔部分進行了討論 26.6.3.7.處理可變參數.

This is discussed in the documentation section 26.6.3.7. Coping with mutable arguments.

不幸的是,他們對這個問題并沒有任何優雅的解決方案!推薦的解決方法是使用 side_effect 從可變參數中復制元素.

Unfortunately, they don't really have any elegant solution to the issue! The recommended workaround is copying elements from the mutable arguments by using side_effect.

如果您為模擬提供了一個 side_effect 函數,那么將使用與模擬相同的參數調用 side_effect.這使我們有機會復制參數并將它們存儲起來以供以后斷言.

If you provide a side_effect function for a mock then side_effect will be called with the same args as the mock. This gives us an opportunity to copy the arguments and store them for later assertions.

在我看來,實施起來有些混亂.如果您在多個地方需要該功能,您可能更愿意繼承 Mock 并直接添加該功能:

It's somewhat messy to implement, in my opinion. If you need the capability in multiple places, you may prefer to subclass Mock and add the feature directly:

from copy import deepcopy

class CopyingMock(MagicMock):
    def __call__(self, *args, **kwargs):
        args = deepcopy(args)
        kwargs = deepcopy(kwargs)
        return super(CopyingMock, self).__call__(*args, **kwargs)

<小時>

2017:它現在可以在第三方發行版中使用(pip install copyingmock).


2017: It's now available in a third-party distribution (pip install copyingmock).

>>> from copyingmock import CopyingMock
>>> mock = CopyingMock()
>>> list_ = [1,2]
>>> mock(list_)
<CopyingMock name='mock()' id='4366094008'>
>>> list_.append(3)
>>> mock.assert_called_once_with([1,2])
>>> mock.assert_called_once_with(list_)

AssertionError: Expected call: mock([1, 2, 3])
Actual call: mock([1, 2])

這篇關于我們如何確保 Mock.call_args_list 中的調用包含具有與調用 Mock 對象時相同狀態的參數的調用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
主站蜘蛛池模板: 成人在线视频观看 | 青青草中文字幕 | 成人精品国产免费网站 | 国产精品久久久久久久久久久久久 | 91av视频在线免费观看 | 日韩一区二区在线视频 | 四虎精品在线 | 九九精品在线 | 日韩中文字幕在线播放 | 久久se精品一区精品二区 | 久产久精国产品 | 91网在线播放| 九九综合 | 亚洲一区精品在线 | 精品一区二区三区在线观看 | 国产精品三级 | 国内毛片毛片毛片毛片 | 亚洲精品一区中文字幕乱码 | 欧美日韩在线精品 | 蜜桃免费一区二区三区 | 黄色在线免费网站 | 欧美日韩在线观看一区 | 欧美美乳 | 天天看天天干 | 欧美一区二区三区免费在线观看 | 看片网站在线 | 国产精品乱码一二三区的特点 | 亚洲综合大片69999 | 久久亚洲综合 | 91精品国产一区二区三区 | 人人做人人澡人人爽欧美 | 精品国产伦一区二区三区观看说明 | 成人在线精品 | 久久精品国产99国产 | 亚洲视频免费观看 | 91免费在线 | 亚洲福利在线观看 | 欧美日韩亚洲一区 | 在线成人www免费观看视频 | 亚洲一区二区欧美 | 91综合网|