summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2015-04-14 07:12:57 (GMT)
committerŁukasz Langa <lukasz@langa.pl>2015-04-14 07:12:57 (GMT)
commit5f6684efe0690c1272b206764301e751e37d9bcf (patch)
treeba6a1f57f1603b2f3af243de631c0abbd0de6a95
parent4882cacab6bcaf90ac496fc8878987598e7665c1 (diff)
downloadcpython-5f6684efe0690c1272b206764301e751e37d9bcf.zip
cpython-5f6684efe0690c1272b206764301e751e37d9bcf.tar.gz
cpython-5f6684efe0690c1272b206764301e751e37d9bcf.tar.bz2
Issue #23310: Fix MagicMock's initializer to work with __methods__.
Behavior equivalent to m.configure_mock(). Patch by Kasia Jachim.
-rw-r--r--Lib/unittest/mock.py5
-rw-r--r--Lib/unittest/test/testmock/testmagicmethods.py15
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
4 files changed, 22 insertions, 2 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index dcb2d8f..1b1c9f4 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1771,14 +1771,15 @@ def _set_return_value(mock, method, name):
class MagicMixin(object):
def __init__(self, *args, **kw):
+ self._mock_set_magics() # make magic work for kwargs in init
_safe_super(MagicMixin, self).__init__(*args, **kw)
- self._mock_set_magics()
+ self._mock_set_magics() # fix magic broken by upper level init
def _mock_set_magics(self):
these_magics = _magics
- if self._mock_methods is not None:
+ if getattr(self, "_mock_methods", None) is not None:
these_magics = _magics.intersection(self._mock_methods)
remove_magics = set()
diff --git a/Lib/unittest/test/testmock/testmagicmethods.py b/Lib/unittest/test/testmock/testmagicmethods.py
index 73b717d..e05c6e0 100644
--- a/Lib/unittest/test/testmock/testmagicmethods.py
+++ b/Lib/unittest/test/testmock/testmagicmethods.py
@@ -439,5 +439,20 @@ class TestMockingMagicMethods(unittest.TestCase):
bar_direct = m.__rdivmod__(2)
self.assertIsInstance(bar_direct, MagicMock)
+ # http://bugs.python.org/issue23310
+ # Check if you can change behaviour of magic methds in MagicMock init
+ def test_magic_in_initialization(self):
+ m = MagicMock(**{'__str__.return_value': "12"})
+ self.assertEqual(str(m), "12")
+
+ def test_changing_magic_set_in_initialization(self):
+ m = MagicMock(**{'__str__.return_value': "12"})
+ m.__str__.return_value = "13"
+ self.assertEqual(str(m), "13")
+ m = MagicMock(**{'__str__.return_value': "12"})
+ m.configure_mock(**{'__str__.return_value': "14"})
+ self.assertEqual(str(m), "14")
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Misc/ACKS b/Misc/ACKS
index e0ade24..7de6de6 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -632,6 +632,7 @@ John Interrante
Bob Ippolito
Roger Irwin
Atsuo Ishimoto
+Kasia Jachim
Adam Jackson
Ben Jackson
Paul Jackson
diff --git a/Misc/NEWS b/Misc/NEWS
index a38f395..54f6330 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -173,6 +173,9 @@ Library
- Issue #23801: Fix issue where cgi.FieldStorage did not always ignore the
entire preamble to a multipart body.
+- Issue #23310: Fix MagicMock's initializer to work with __methods__, just
+ like configure_mock(). Patch by Kasia Jachim.
+
Tests
-----