]> granicus.if.org Git - python/commitdiff
bpo-37828: Fix default mock_name in unittest.mock.assert_called error (GH-16166)
authorAbraham Toriz Cruz <awonderfulcode@gmail.com>
Tue, 17 Sep 2019 11:16:08 +0000 (06:16 -0500)
committerPablo Galindo <Pablogsal@gmail.com>
Tue, 17 Sep 2019 11:16:08 +0000 (12:16 +0100)
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').

Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS.d/next/Library/2019-09-15-21-31-18.bpo-37828.gLLDX7.rst [new file with mode: 0644]

index 74d32af9bf99862757b08f803f1c6cb5c19f4f4d..4cf8e60ccc15df653a7258e6cfb30d90aeda453d 100644 (file)
@@ -868,7 +868,7 @@ class NonCallableMock(Base):
         """
         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):
index 581afaaeb815dfe42298079e1ed39ae80da52539..2bafa8266b63ba59c07ef768d7a1ea86c6087f24 100644 (file)
@@ -396,6 +396,14 @@ class MockTest(unittest.TestCase):
         _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()
diff --git a/Misc/NEWS.d/next/Library/2019-09-15-21-31-18.bpo-37828.gLLDX7.rst b/Misc/NEWS.d/next/Library/2019-09-15-21-31-18.bpo-37828.gLLDX7.rst
new file mode 100644 (file)
index 0000000..c364009
--- /dev/null
@@ -0,0 +1,2 @@
+Fix default mock name in :meth:`unittest.mock.Mock.assert_called` exceptions.
+Patch by Abraham Toriz Cruz.