mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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
This commit is contained in:
parent
bb6bf7d342
commit
b5a7a4f0c2
4 changed files with 64 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
|||
import asyncio
|
||||
import inspect
|
||||
import re
|
||||
import unittest
|
||||
|
||||
from unittest.mock import (ANY, call, AsyncMock, patch, MagicMock,
|
||||
|
@ -889,3 +890,23 @@ class AsyncMockAssert(unittest.TestCase):
|
|||
asyncio.run(self._runnable_test())
|
||||
with self.assertRaises(AssertionError):
|
||||
self.mock.assert_not_awaited()
|
||||
|
||||
def test_assert_has_awaits_not_matching_spec_error(self):
|
||||
async def f(): pass
|
||||
|
||||
mock = AsyncMock(spec=f)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
AssertionError,
|
||||
re.escape('Awaits not found.\nExpected:')) as cm:
|
||||
mock.assert_has_awaits([call()])
|
||||
self.assertIsNone(cm.exception.__cause__)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
AssertionError,
|
||||
re.escape('Error processing expected awaits.\n'
|
||||
"Errors: [None, TypeError('too many positional "
|
||||
"arguments')]\n"
|
||||
'Expected:')) as cm:
|
||||
mock.assert_has_awaits([call(), call('wrong')])
|
||||
self.assertIsInstance(cm.exception.__cause__, TypeError)
|
||||
|
|
|
@ -1435,6 +1435,25 @@ class MockTest(unittest.TestCase):
|
|||
mock.assert_has_calls(calls[:-1])
|
||||
mock.assert_has_calls(calls[:-1], any_order=True)
|
||||
|
||||
def test_assert_has_calls_not_matching_spec_error(self):
|
||||
def f(): pass
|
||||
|
||||
mock = Mock(spec=f)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
AssertionError,
|
||||
re.escape('Calls not found.\nExpected:')) as cm:
|
||||
mock.assert_has_calls([call()])
|
||||
self.assertIsNone(cm.exception.__cause__)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
AssertionError,
|
||||
re.escape('Error processing expected calls.\n'
|
||||
"Errors: [None, TypeError('too many positional "
|
||||
"arguments')]\n"
|
||||
'Expected:')) as cm:
|
||||
mock.assert_has_calls([call(), call('wrong')])
|
||||
self.assertIsInstance(cm.exception.__cause__, TypeError)
|
||||
|
||||
def test_assert_any_call(self):
|
||||
mock = Mock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue