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

我們?nèi)绾未_保 Mock.call_args_list 中的調(diào)用包含具有

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?(我們?nèi)绾未_保 Mock.call_args_list 中的調(diào)用包含具有與調(diào)用 Mock 對(duì)象時(shí)相同狀態(tài)的
本文介紹了我們?nèi)绾未_保 Mock.call_args_list 中的調(diào)用包含具有與調(diào)用 Mock 對(duì)象時(shí)相同狀態(tài)的參數(shù)的調(diào)用?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

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

我的問(wèn)題是如何確保 u.call_args_list 中的調(diào)用在調(diào)用模擬時(shí)而不是在檢查模擬參數(shù)時(shí)包含所有對(duì)象的狀態(tài)?

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.

推薦答案

這在文檔部分進(jìn)行了討論 26.6.3.7.處理可變參數(shù).

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

不幸的是,他們對(duì)這個(gè)問(wèn)題并沒(méi)有任何優(yōu)雅的解決方案!推薦的解決方法是使用 side_effect 從可變參數(shù)中復(fù)制元素.

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.

如果您為模擬提供了一個(gè) side_effect 函數(shù),那么將使用與模擬相同的參數(shù)調(diào)用 side_effect.這使我們有機(jī)會(huì)復(fù)制參數(shù)并將它們存儲(chǔ)起來(lái)以供以后斷言.

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.

在我看來(lái),實(shí)施起來(lái)有些混亂.如果您在多個(gè)地方需要該功能,您可能更愿意繼承 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)

<小時(shí)>

2017:它現(xiàn)在可以在第三方發(fā)行版中使用(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])

這篇關(guān)于我們?nèi)绾未_保 Mock.call_args_list 中的調(diào)用包含具有與調(diào)用 Mock 對(duì)象時(shí)相同狀態(tài)的參數(shù)的調(diào)用?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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 only a single method on an object(僅模擬對(duì)象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
主站蜘蛛池模板: 99精品国产一区二区三区 | 一区二区久久 | 久久精品国产99国产精品 | 99久久精品视频免费 | 99re热精品视频 | 久久国产婷婷国产香蕉 | 国产精品久久久久久久三级 | 国产精品乱码一二三区的特点 | 国产在线精品一区 | 久久国产精品-国产精品 | 国产成人在线观看免费 | 欧美最猛性xxxxx亚洲精品 | 亚洲精品久久久9婷婷中文字幕 | 天堂av中文在线 | 精品国产一区二区三区久久影院 | 午夜资源 | 色婷婷一区 | 阿v视频在线观看 | 亚洲传媒在线 | 亚洲a视| 能看的av网站 | 99国产在线 | 亚洲国产区 | 激情欧美一区二区三区中文字幕 | 成人三级在线观看 | 91精品国产一区二区三区 | 色婷婷av777 av免费网站在线 | 在线黄av| 欧美精品一区二区三区四区 | 久久久成人精品 | 日韩精品在线观看一区二区 | 一级黄色毛片免费 | 精品日本久久久久久久久久 | 91精品国产一二三 | 亚洲精品第一国产综合野 | 国产999精品久久久久久 | 国产一区二区三区久久久久久久久 | 日韩一区二区三区在线视频 | 亚洲69p| 午夜视频一区 | 日韩免费视频一区二区 |