MCPcopy Index your code
hub / github.com/python/cpython / assert_has_calls

Method assert_has_calls

Lib/unittest/mock.py:1001–1043  ·  view source on GitHub ↗

assert the mock has been called with the specified calls. The `mock_calls` list is checked for the calls. If `any_order` is False (the default) then the calls must be sequential. There can be extra calls before or after the specified calls. If `any_order` is

(self, calls, any_order=False)

Source from the content-addressed store, hash-verified

999
1000
1001 def assert_has_calls(self, calls, any_order=False):
1002 """assert the mock has been called with the specified calls.
1003 The `mock_calls` list is checked for the calls.
1004
1005 If `any_order` is False (the default) then the calls must be
1006 sequential. There can be extra calls before or after the
1007 specified calls.
1008
1009 If `any_order` is True then the calls can be in any order, but
1010 they must all appear in `mock_calls`."""
1011 expected = [self._call_matcher(c) for c in calls]
1012 cause = next((e for e in expected if isinstance(e, Exception)), None)
1013 all_calls = _CallList(self._call_matcher(c) for c in self.mock_calls)
1014 if not any_order:
1015 if expected not in all_calls:
1016 if cause is None:
1017 problem = 'Calls not found.'
1018 else:
1019 problem = ('Error processing expected calls.\n'
1020 'Errors: {}').format(
1021 [e if isinstance(e, Exception) else None
1022 for e in expected])
1023 raise AssertionError(
1024 f'{problem}\n'
1025 f'Expected: {_CallList(calls)}\n'
1026 f' Actual: {safe_repr(self.mock_calls)}'
1027 ) from cause
1028 return
1029
1030 all_calls = list(all_calls)
1031
1032 not_found = []
1033 for kall in expected:
1034 try:
1035 all_calls.remove(kall)
1036 except ValueError:
1037 not_found.append(kall)
1038 if not_found:
1039 raise AssertionError(
1040 '%r does not contain all of %r in its call list, '
1041 'found %r instead' % (self._mock_name or 'mock',
1042 tuple(not_found), all_calls)
1043 ) from cause
1044
1045
1046 def assert_any_call(self, /, *args, **kwargs):

Calls 7

_call_matcherMethod · 0.95
safe_reprFunction · 0.90
_CallListClass · 0.85
listClass · 0.85
formatMethod · 0.45
removeMethod · 0.45
appendMethod · 0.45