Behavior equivalent to m.configure_mock(). Patch by Kasia Jachim.
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()
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()
Bob Ippolito
Roger Irwin
Atsuo Ishimoto
+Kasia Jachim
Adam Jackson
Ben Jackson
Paul Jackson
- 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
-----