Skip to content

Commit f668d2b

Browse files
miss-islingtonAbraham Toriz Cruz
andauthored
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 5f5f11f) Co-authored-by: Abraham Toriz Cruz <awonderfulcode@gmail.com>
1 parent 728bea6 commit f668d2b

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ def assert_called(self):
873873
"""
874874
if self.call_count == 0:
875875
msg = ("Expected '%s' to have been called." %
876-
self._mock_name or 'mock')
876+
(self._mock_name or 'mock'))
877877
raise AssertionError(msg)
878878

879879
def assert_called_once(self):

Lib/unittest/test/testmock/testmock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ def _check(mock):
388388
_check(mock)
389389

390390

391+
def test_assert_called_exception_message(self):
392+
msg = "Expected '{0}' to have been called"
393+
with self.assertRaisesRegex(AssertionError, msg.format('mock')):
394+
Mock().assert_called()
395+
with self.assertRaisesRegex(AssertionError, msg.format('test_name')):
396+
Mock(name="test_name").assert_called()
397+
398+
391399
def test_assert_called_once_with(self):
392400
mock = Mock()
393401
mock()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix default mock name in :meth:`unittest.mock.Mock.assert_called` exceptions.
2+
Patch by Abraham Toriz Cruz.

0 commit comments

Comments
 (0)