简体   繁体   中英

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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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