Skip to content

[3.8] bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166) #16229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,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 @@ -388,6 +388,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.