Way*_*ner 5 python mocking python-3.x python-mock
考虑以下文件:
圣手手榴弹.py
def count(one, two, five='three'):
print('boom')
Run Code Online (Sandbox Code Playgroud)
test_holy_hand_grenade.py
from unittest import mock
import holy_hand_grenade
def test_hand_grenade():
mock_count = mock.patch("holy_hand_grenade.count", autospec=True)
with mock_count as fake_count:
fake_count(1, 2, five=5)
# According to https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args
# this should work
assert fake_count.call_args.kwargs['five'] == 5
Run Code Online (Sandbox Code Playgroud)
根据文档,call_args
应该是:
这要么是 None (如果尚未调用模拟),要么是最后一次调用模拟的参数。这将采用元组的形式:第一个成员,也可以通过 args 属性访问,是调用模拟的任何有序参数(或空元组),第二个成员,也可以通过访问kwargs 属性是任何关键字参数(或空字典)。
(强调我的)
但这在我的脸上炸开了锅 TypeError: tuple indices must be integers or slices, not str
嗯。不?
我真的不明白的是,如果这是一个调用对象,它就是,因为
assert isinstance(fake_count.call_args, (mock._Call,))
Run Code Online (Sandbox Code Playgroud)
通过,它应该有 kwargs 和 args。它……嗯,确实如此。但它们似乎实际上并不是正确的:
assert isinstance(fake_count.call_args.kwargs, (mock._Call,)) #this works
assert isinstance(fake_count.call_args.kwargs, (dict,)) # doesn't work
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
归档时间: |
|
查看次数: |
2324 次 |
最近记录: |