Skip to content

Commit 5f5f11f

Browse files
Abraham Toriz Cruzpablogsal
authored andcommitted
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').
1 parent 219fb9d commit 5f5f11f

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
@@ -868,7 +868,7 @@ def assert_called(self):
868868
"""
869869
if self.call_count == 0:
870870
msg = ("Expected '%s' to have been called." %
871-
self._mock_name or 'mock')
871+
(self._mock_name or 'mock'))
872872
raise AssertionError(msg)
873873

874874
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
@@ -396,6 +396,14 @@ def _check(mock):
396396
_check(mock)
397397

398398

399+
def test_assert_called_exception_message(self):
400+
msg = "Expected '{0}' to have been called"
401+
with self.assertRaisesRegex(AssertionError, msg.format('mock')):
402+
Mock().assert_called()
403+
with self.assertRaisesRegex(AssertionError, msg.format('test_name')):
404+
Mock(name="test_name").assert_called()
405+
406+
399407
def test_assert_called_once_with(self):
400408
mock = Mock()
401409
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)