bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166)

In the format string for assert_called the evaluation order is incorrect and hence for mock's without name, 'None' is printed whereas it should be 'mock' like for other messages. The error message is ("Expected '%s' to have been called." % self._mock_name or 'mock').
(cherry picked from commit 5f5f11faf9)

Co-authored-by: Abraham Toriz Cruz <awonderfulcode@gmail.com>
This commit is contained in:
Miss Islington (bot) 2019-09-17 04:35:56 -07:00 committed by GitHub
parent 728bea60e5
commit f668d2b775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View File

@ -873,7 +873,7 @@ class NonCallableMock(Base):
"""
if self.call_count == 0:
msg = ("Expected '%s' to have been called." %
self._mock_name or 'mock')
(self._mock_name or 'mock'))
raise AssertionError(msg)
def assert_called_once(self):

View File

@ -388,6 +388,14 @@ class MockTest(unittest.TestCase):
_check(mock)
def test_assert_called_exception_message(self):
msg = "Expected '{0}' to have been called"
with self.assertRaisesRegex(AssertionError, msg.format('mock')):
Mock().assert_called()
with self.assertRaisesRegex(AssertionError, msg.format('test_name')):
Mock(name="test_name").assert_called()
def test_assert_called_once_with(self):
mock = Mock()
mock()

View File

@ -0,0 +1,2 @@
Fix default mock name in :meth:`unittest.mock.Mock.assert_called` exceptions.
Patch by Abraham Toriz Cruz.