【问题标题】:Why am I getting a TypeError when running assert_has_calls()?为什么在运行 assert_has_calls() 时会出现 TypeError?
【发布时间】:2021-12-11 04:55:56
【问题描述】:

我正在尝试在通过 @patch 装饰器使用 pytest 模拟时进行简单的 assert_has_calls() 调用。相反,我收到一条错误消息。下面是我在测试脚本中运行的代码的 sn-p。

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:\users\jmt3e\anaconda3\lib\unittest\mock.py:211: in assert_has_calls
    return mock.assert_has_calls(*args, **kwargs)
c:\users\jmt3e\anaconda3\lib\unittest\mock.py:938: in assert_has_calls
    expected = [self._call_matcher(c) for c in calls]
c:\users\jmt3e\anaconda3\lib\unittest\mock.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:\users\jmt3e\anaconda3\lib\unittest\mock.py:854: TypeError

【问题讨论】:

标签: python mocking pytest patch assert


【解决方案1】:

好的.. 问题解决了.. 原来问题是我没有从 unittest.mock 导入“调用”。

对于那些可能遇到相同问题的人,请务必导入此文件,否则您将花费​​几个小时来找出代码中的问题。我应该更彻底地阅读文档,但是在有关您必须导入“调用”的不同“断言”部分中并不完全清楚。

如果他们在这里给出的示例代码 sn-ps 会很好:https://docs.python.org/3/library/unittest.mock.html#call 为清楚起见,将列出导入行。事实上,据我所知,没有提到“导入调用”。我弄清楚这一点的唯一方法是查找其他有问题的人并注意导入。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多