Commit 5f5f11fa authored by Abraham Toriz Cruz's avatar Abraham Toriz Cruz Committed by Pablo Galindo

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').
parent 219fb9d6
...@@ -868,7 +868,7 @@ class NonCallableMock(Base): ...@@ -868,7 +868,7 @@ class NonCallableMock(Base):
""" """
if self.call_count == 0: if self.call_count == 0:
msg = ("Expected '%s' to have been called." % msg = ("Expected '%s' to have been called." %
self._mock_name or 'mock') (self._mock_name or 'mock'))
raise AssertionError(msg) raise AssertionError(msg)
def assert_called_once(self): def assert_called_once(self):
......
...@@ -396,6 +396,14 @@ class MockTest(unittest.TestCase): ...@@ -396,6 +396,14 @@ class MockTest(unittest.TestCase):
_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): def test_assert_called_once_with(self):
mock = Mock() mock = Mock()
mock() mock()
......
Fix default mock name in :meth:`unittest.mock.Mock.assert_called` exceptions.
Patch by Abraham Toriz Cruz.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment