Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,8 @@ def assert_has_calls(self, calls, any_order=False):
for e in expected])
raise AssertionError(
f'{problem}\n'
f'Expected: {_CallList(calls)}\n'
f'Actual: {self._calls_repr(prefix="Actual")}'
f'Expected: {_CallList(calls)}'
f'{self._calls_repr(prefix="Actual").rstrip(".")}'
) from cause
return

Expand Down
25 changes: 16 additions & 9 deletions Lib/unittest/test/testmock/testasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,21 +892,28 @@ def test_assert_not_awaited(self):
self.mock.assert_not_awaited()

def test_assert_has_awaits_not_matching_spec_error(self):
async def f(): pass
async def f(x=None): pass

mock = AsyncMock(spec=f)
self.mock = AsyncMock(spec=f)
asyncio.run(self._runnable_test(1))

with self.assertRaisesRegex(
AssertionError,
re.escape('Awaits not found.\nExpected:')) as cm:
mock.assert_has_awaits([call()])
'^{}$'.format(
re.escape('Awaits not found.\n'
'Expected: [call()]\n'
'Actual: [call(1)]'))) as cm:
self.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')])
'^{}$'.format(
re.escape(
'Error processing expected awaits.\n'
"Errors: [None, TypeError('too many positional "
"arguments')]\n"
'Expected: [call(), call(1, 2)]\n'
'Actual: [call(1)]'))) as cm:
self.mock.assert_has_awaits([call(), call(1, 2)])
self.assertIsInstance(cm.exception.__cause__, TypeError)
21 changes: 14 additions & 7 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,23 +1436,30 @@ def f(a, b, c, d=None): pass
mock.assert_has_calls(calls[:-1], any_order=True)

def test_assert_has_calls_not_matching_spec_error(self):
def f(): pass
def f(x=None): pass

mock = Mock(spec=f)
mock(1)

with self.assertRaisesRegex(
AssertionError,
re.escape('Calls not found.\nExpected:')) as cm:
'^{}$'.format(
re.escape('Calls not found.\n'
'Expected: [call()]\n'
'Actual: [call(1)]'))) 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')])
'^{}$'.format(
re.escape(
'Error processing expected calls.\n'
"Errors: [None, TypeError('too many positional arguments')]\n"
"Expected: [call(), call(1, 2)]\n"
'Actual: [call(1)]'))) as cm:
mock.assert_has_calls([call(), call(1, 2)])
self.assertIsInstance(cm.exception.__cause__, TypeError)

def test_assert_any_call(self):
Expand Down