summaryrefslogtreecommitdiff
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorSamuel Freilich <sfreilich@google.com>2019-09-24 15:08:31 -0400
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-09-24 12:08:31 -0700
commitb5a7a4f0c20717a4c92c371583b5521b83f40f32 (patch)
treef3f4df016736fca9503e1a8a6c28dc10222bbb43 /Lib/unittest/mock.py
parentbb6bf7d342b4503a6227fd209fac934905b6a1aa (diff)
downloadcpython-git-b5a7a4f0c20717a4c92c371583b5521b83f40f32.tar.gz
bpo-36871: Handle spec errors in assert_has_calls (GH-16005)
The fix in PR 13261 handled the underlying issue about the spec for specific methods not being applied correctly, but it didn't fix the issue that was causing the misleading error message. The code currently grabs a list of responses from _call_matcher (which may include exceptions). But it doesn't reach inside the list when checking if the result is an exception. This results in a misleading error message when one of the provided calls does not match the spec. https://bugs.python.org/issue36871 Automerge-Triggered-By: @gpshead
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py26
1 files changed, 21 insertions, 5 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 22d63a4588..7bd11c8e70 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -926,13 +926,21 @@ class NonCallableMock(Base):
If `any_order` is True then the calls can be in any order, but
they must all appear in `mock_calls`."""
expected = [self._call_matcher(c) for c in calls]
- cause = expected if isinstance(expected, Exception) else None
+ cause = next((e for e in expected if isinstance(e, Exception)), None)
all_calls = _CallList(self._call_matcher(c) for c in self.mock_calls)
if not any_order:
if expected not in all_calls:
+ if cause is None:
+ problem = 'Calls not found.'
+ else:
+ problem = ('Error processing expected calls.\n'
+ 'Errors: {}').format(
+ [e if isinstance(e, Exception) else None
+ for e in expected])
raise AssertionError(
- 'Calls not found.\nExpected: %r%s'
- % (_CallList(calls), self._calls_repr(prefix="Actual"))
+ f'{problem}\n'
+ f'Expected: {_CallList(calls)}\n'
+ f'Actual: {self._calls_repr(prefix="Actual")}'
) from cause
return
@@ -2244,12 +2252,20 @@ class AsyncMockMixin(Base):
they must all appear in :attr:`await_args_list`.
"""
expected = [self._call_matcher(c) for c in calls]
- cause = expected if isinstance(expected, Exception) else None
+ cause = next((e for e in expected if isinstance(e, Exception)), None)
all_awaits = _CallList(self._call_matcher(c) for c in self.await_args_list)
if not any_order:
if expected not in all_awaits:
+ if cause is None:
+ problem = 'Awaits not found.'
+ else:
+ problem = ('Error processing expected awaits.\n'
+ 'Errors: {}').format(
+ [e if isinstance(e, Exception) else None
+ for e in expected])
raise AssertionError(
- f'Awaits not found.\nExpected: {_CallList(calls)}\n'
+ f'{problem}\n'
+ f'Expected: {_CallList(calls)}\n'
f'Actual: {self.await_args_list}'
) from cause
return