[当将mock
的call()
对象与assert_has_calls
一起使用时,我正在努力断言已使用给定的字符串并在末尾附加了未知值。
例如:
测试中的代码:
mystring = 'a known string with an unknown value: {0}'.format(unknown_value)
method_to_call(mystring)
当前测试代码:
with mock.patch('method_to_call') as mocked_method:
calls = [call('a known string with and unknown value: {0}'.format(mock.ANY)]
call_method()
mocked_method.assert_has_calls(calls)
这给了我一些类似的东西:
AssertionError: Calls not found.
Expected: [call('a known string with and unknown value: <ANY>')]
我如何断言给定的字符串已传递给方法,但允许未知值?
您可以使用callee
lib对方法调用中使用的字符串进行部分匹配:
callee
或者,如果您不想添加另一个库,并且确定知道调用的顺序,则可以从调用参数中提取字符串,然后使用正则表达式进行匹配
from callee import String, StartsWith
with mock.patch('method_to_call') as mocked_method:
call_method()
mocked_method.assert_called_with(String() & StartsWith('a known string with an unknown value: '))