Commit 2180f6b0 authored by Samuel Freilich's avatar Samuel Freilich Committed by Gregory P. Smith

bpo-36871: Avoid duplicated 'Actual:' in assertion message (GH-16361)

Fixes an issue caught after merge of PR 16005.

Tightened test assertions to check the entire assertion message.
parent b5a7a4f0
...@@ -939,8 +939,8 @@ class NonCallableMock(Base): ...@@ -939,8 +939,8 @@ class NonCallableMock(Base):
for e in expected]) for e in expected])
raise AssertionError( raise AssertionError(
f'{problem}\n' f'{problem}\n'
f'Expected: {_CallList(calls)}\n' f'Expected: {_CallList(calls)}'
f'Actual: {self._calls_repr(prefix="Actual")}' f'{self._calls_repr(prefix="Actual").rstrip(".")}'
) from cause ) from cause
return return
......
...@@ -892,21 +892,28 @@ class AsyncMockAssert(unittest.TestCase): ...@@ -892,21 +892,28 @@ class AsyncMockAssert(unittest.TestCase):
self.mock.assert_not_awaited() self.mock.assert_not_awaited()
def test_assert_has_awaits_not_matching_spec_error(self): 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( with self.assertRaisesRegex(
AssertionError, AssertionError,
re.escape('Awaits not found.\nExpected:')) as cm: '^{}$'.format(
mock.assert_has_awaits([call()]) 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__) self.assertIsNone(cm.exception.__cause__)
with self.assertRaisesRegex( with self.assertRaisesRegex(
AssertionError, AssertionError,
re.escape('Error processing expected awaits.\n' '^{}$'.format(
"Errors: [None, TypeError('too many positional " re.escape(
"arguments')]\n" 'Error processing expected awaits.\n'
'Expected:')) as cm: "Errors: [None, TypeError('too many positional "
mock.assert_has_awaits([call(), call('wrong')]) "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) self.assertIsInstance(cm.exception.__cause__, TypeError)
...@@ -1436,23 +1436,30 @@ class MockTest(unittest.TestCase): ...@@ -1436,23 +1436,30 @@ class MockTest(unittest.TestCase):
mock.assert_has_calls(calls[:-1], any_order=True) mock.assert_has_calls(calls[:-1], any_order=True)
def test_assert_has_calls_not_matching_spec_error(self): def test_assert_has_calls_not_matching_spec_error(self):
def f(): pass def f(x=None): pass
mock = Mock(spec=f) mock = Mock(spec=f)
mock(1)
with self.assertRaisesRegex( with self.assertRaisesRegex(
AssertionError, 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()]) mock.assert_has_calls([call()])
self.assertIsNone(cm.exception.__cause__) self.assertIsNone(cm.exception.__cause__)
with self.assertRaisesRegex( with self.assertRaisesRegex(
AssertionError, AssertionError,
re.escape('Error processing expected calls.\n' '^{}$'.format(
"Errors: [None, TypeError('too many positional " re.escape(
"arguments')]\n" 'Error processing expected calls.\n'
'Expected:')) as cm: "Errors: [None, TypeError('too many positional arguments')]\n"
mock.assert_has_calls([call(), call('wrong')]) "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) self.assertIsInstance(cm.exception.__cause__, TypeError)
def test_assert_any_call(self): def test_assert_any_call(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment