summaryrefslogtreecommitdiff
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2019-09-24 22:29:17 -0700
committerGitHub <noreply@github.com>2019-09-24 22:29:17 -0700
commit4042e1afd252858de53e2b79d946a51a0182d1ba (patch)
tree361a74856d7a8dd4e206b6c33b6f4be124d9d92f /Lib/unittest/mock.py
parent16c0f6df62a39f9f7712b1c0577de4eefcb4c1bf (diff)
downloadcpython-git-4042e1afd252858de53e2b79d946a51a0182d1ba.tar.gz
[3.7] bpo-36871: Handle spec errors in assert_has_calls (GH-16364) (GH-16374)
Handle spec errors in assert_has_calls (GH-16005) (GH-16364) 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 Co-authored-by: Samuel Freilich <sfreilich@google.com> (cherry picked from commit 1a17a054f6314ce29cd2632c28aeed317a615360)
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index b1ab8631a4..2b9e7f14a7 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -895,13 +895,20 @@ 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\n'
- 'Actual: %r' % (_CallList(calls), self.mock_calls)
+ '%s\nExpected: %r\nActual: %r' % (
+ problem, _CallList(calls), self.mock_calls)
) from cause
return