为什么我得到一个TypeError时运行assert_has_calls()?



我试图通过@patch装饰器使用pytest模拟时进行简单的assert_has_calls()调用。相反,我得到一个错误消息。下面是我在测试脚本中运行的代码片段。

calls = [mock_draw_discard_pile_attempt_check_meld_match(P2, True), mock_draw_discard_pile_attempt_check_meld_match(P2)]
mock_draw_discard_pile_attempt_check_meld_match.assert_has_calls(calls)

我得到的错误信息如下。

draw_discard_pile_attempt(P2, True)
mock_discard_pile_is_frozen.assert_called()
calls = [mock_draw_discard_pile_attempt_check_meld_match(P2, True), mock_draw_discard_pile_attempt_check_meld_match(P2)]
>       mock_draw_discard_pile_attempt_check_meld_match.assert_has_calls(calls)
test_Canasta_replica.py:361:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
c:usersjmt3eanaconda3libunittestmock.py:211: in assert_has_calls
return mock.assert_has_calls(*args, **kwargs)
c:usersjmt3eanaconda3libunittestmock.py:938: in assert_has_calls
expected = [self._call_matcher(c) for c in calls]
c:usersjmt3eanaconda3libunittestmock.py:938: in <listcomp>
expected = [self._call_matcher(c) for c in calls]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <MagicMock name='draw_discard_pile_attempt_check_meld_match' spec='function' id='2654848555088'>, _call = None
def _call_matcher(self, _call):
"""
Given a call (or simply an (args, kwargs) tuple), return a
comparison key suitable for matching with other calls.
This is a best effort method which relies on the spec's signature,
if available, or falls back on the arguments themselves.
"""
if isinstance(_call, tuple) and len(_call) > 2:
sig = self._get_call_signature_from_name(_call[0])
else:
sig = self._spec_signature
if sig is not None:
>           if len(_call) == 2:
E           TypeError: object of type 'NoneType' has no len()
c:usersjmt3eanaconda3libunittestmock.py:854: TypeError

好的…问题解决了. .原来问题是我没有从unittest.mock导入'call' .

对于那些可能有同样问题的人,确保导入这个,否则您将花费几个小时试图找出代码中的错误。我本应该更彻底地阅读文档,但在关于必须导入'call'的不同'断言'的部分中并不完全清楚。

如果在这里给出的示例代码片段中:https://docs.python.org/3/library/unittest.mock.html#call为了清楚起见,将列出进口行。事实上,据我所知,"import call"并没有被提及。我发现这个问题的唯一方法是查找其他人遇到的问题,并注意到导入。

如果很清楚,我错过了文档,请忽略。

相关内容

  • 没有找到相关文章

最新更新