Skip to content

Commit

Permalink
bpo-37828: Fix default mock_name in unittest.mock.assert_called error (
Browse files Browse the repository at this point in the history
…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').
  • Loading branch information
categulario authored and pablogsal committed Sep 17, 2019
1 parent 219fb9d commit 5f5f11f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def assert_called(self):
"""
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):
Expand Down
8 changes: 8 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ def _check(mock):
_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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix default mock name in :meth:`unittest.mock.Mock.assert_called` exceptions.
Patch by Abraham Toriz Cruz.

0 comments on commit 5f5f11f

Please sign in to comment.