When trying to unit test a method that returns a tuple, and I try to check if the code accesses the correct tuple index, python tries to evaluate the expected call and turns it into a string.
call().methodA().__getitem__(0) ends up converting to '().methodA' in my expected_calls list for approval.
The above code example gives the result and trace:
expected_calls=[call().methodA(), '().methodA'] result_calls=[call().methodA(), call().methodA().__getitem__(0)] ====================================================================== ERROR: test_methodB (badMockCalls.Test_UsingToBeMocked_methods) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\dev\workspace\TestCode\src\badMockCalls.py", line 43, in test_methodB self.assertListEqual(expected_calls, self.result_calls) File "C:\Python33\lib\unittest\case.py", line 844, in assertListEqual self.assertSequenceEqual(list1, list2, msg, seq_type=list) File "C:\Python33\lib\unittest\case.py", line 764, in assertSequenceEqual if seq1 == seq2: File "C:\Python33\lib\unittest\mock.py", line 1927, in __eq__ first, second = other ValueError: too many values to unpack (expected 2) ---------------------------------------------------------------------- Ran 1 test in 0.006s FAILED (errors=1)
How can I state that method B calls self.tbm.methodA () [0] correctly?
Sample code (Python 3.3.2):
import unittest from unittest.mock import call, patch import logging log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) _ch = logging.StreamHandler() _ch.setLevel(logging.DEBUG) log.addHandler(_ch) class ToBeMocked():
Mike h
source share