简体   繁体   English

为什么在运行 assert_has_calls() 时会出现 TypeError?

[英]Why am I getting a TypeError when running assert_has_calls()?

I am trying to make a simple assert_has_calls() call while using a pytest mock via an @patch decorator.我正在尝试通过 @patch 装饰器使用 pytest 模拟进行简单的 assert_has_calls() 调用。 I am instead getting an error message.相反,我收到了一条错误消息。 Below is the snippet of code I'm running in my test script.下面是我在测试脚本中运行的代码片段。

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)

The error message I am getting is below.我收到的错误消息如下。

        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

Okay.. problem solved.. It turns out the issue was that I was not importing 'call' from unittest.mock.好的..问题解决了..事实证明问题是我没有从unittest.mock导入'call'。

For those who may have had the same issue, make sure to import this or you will spend a couple of hours trying to figure out what was wrong in your code.对于那些可能遇到同样问题的人,请确保导入它,否则您将花费​​几个小时试图找出代码中的问题。 I should have been more thorough in reading the documentation, but it's not completely clear in the section about the different 'asserts' that you have to import 'call'.我应该更彻底地阅读文档,但在有关您必须导入“调用”的不同“断言”的部分中并不完全清楚。

It would have been nice if in the example code snippets they give here: https://docs.python.org/3/library/unittest.mock.html#call there would be the import lines listed for clarity.如果在他们在这里给出的示例代码片段中会很好: https : //docs.python.org/3/library/unittest.mock.html#call为了清楚起见,会列出导入行。 In fact, 'import call' is not mentioned, as far as I can see.事实上,据我所知,并没有提到“导入调用”。 The only way I figured this out was by looking up other people having issues with it and noticing the import.我弄清楚这一点的唯一方法是查找其他有问题的人并注意导入。

If it is clear and I missed the documentation, please disregard.如果很清楚并且我错过了文档,请忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM