From 7ea9408816027287b013e5124d9a34962f3d44e8 Mon Sep 17 00:00:00 2001
From: Michael Foord <michael@voidspace.org.uk>
Date: Fri, 28 Sep 2012 16:15:22 +0100
Subject: [PATCH] Closes issue 15323. Improve failure message of
 Mock.assert_called_once_with

---
 NEWS              | 2 ++
 mock.py           | 4 ++--
 tests/testmock.py | 7 +++++++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index 7ad0da6..6b70b69 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,5 @@
+- Issue #15323: improve failure message of Mock.assert_called_once_with
+
 - Issue #14857: fix regression in references to PEP 3135 implicit __class__
   closure variable (Reopens issue #12370)
 
diff --git a/mock.py b/mock.py
index e47f3b1..39b0dd2 100644
--- a/mock.py
+++ b/mock.py
@@ -864,8 +864,8 @@ class NonCallableMock(Base):
         arguments."""
         self = _mock_self
         if not self.call_count == 1:
-            msg = ("Expected to be called once. Called %s times." %
-                   self.call_count)
+            msg = ("Expected '%s' to be called once. Called %s times." %
+                   (self._mock_name or 'mock', self.call_count))
             raise AssertionError(msg)
         return self.assert_called_with(*args, **kwargs)
 
diff --git a/tests/testmock.py b/tests/testmock.py
index b933e90..d788ba0 100644
--- a/tests/testmock.py
+++ b/tests/testmock.py
@@ -486,6 +486,13 @@ class MockTest(unittest.TestCase):
                                 mock.assert_called_with)
 
 
+    def test_assert_called_once_with_message(self):
+        mock = Mock(name='geoffrey')
+        self.assertRaisesRegex(AssertionError,
+                     r"Expected 'geoffrey' to be called once\.",
+                     mock.assert_called_once_with)
+
+
     def test__name__(self):
         mock = Mock()
         self.assertRaises(AttributeError, lambda: mock.__name__)
-- 
GitLab