From 7f0728a93dcadeaf43d50e69b1b718b6b89731a4 Mon Sep 17 00:00:00 2001 From: Michael Foord <michael@voidspace.org.uk> Date: Fri, 27 May 2011 23:09:17 +0100 Subject: [PATCH] Preparation for 0.7.2 --- README.txt | 4 ++-- docs/changelog.txt | 3 +++ docs/getting-started.txt | 28 +++++++++++++++++++++++++++- docs/index.txt | 2 +- mock.py | 13 +++++++++++-- tests/testmagicmethods.py | 35 ++++++++++++++++++----------------- 6 files changed, 62 insertions(+), 23 deletions(-) diff --git a/README.txt b/README.txt index 4b423aa..cdceb99 100644 --- a/README.txt +++ b/README.txt @@ -9,8 +9,8 @@ mock is tested on Python versions 2.4-2.7 and Python 3. The mock module also provides utility functions / objects to assist with testing, particularly monkey patching. -* `PDF documentation for 0.7.1 - <http://www.voidspace.org.uk/downloads/mock-0.7.1.pdf>`_ +* `PDF documentation for 0.7.2 + <http://www.voidspace.org.uk/downloads/mock-0.7.2.pdf>`_ * `mock on google code (repository and issue tracker) <http://code.google.com/p/mock/>`_ * `mock documentation diff --git a/docs/changelog.txt b/docs/changelog.txt index c2e84c6..4b213d7 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -48,6 +48,9 @@ CHANGELOG ------------------------ * BUGFIX: instances of list subclasses can now be used as mock specs +* BUGFIX: MagicMock equality / inequality protocol methods changed to use the + default equality / inequality. This is done through a `side_effect` on + the mocks used for `__eq__` / `__ne__` 2011/05/06 Version 0.7.1 diff --git a/docs/getting-started.txt b/docs/getting-started.txt index 6499367..fe0fcbe 100644 --- a/docs/getting-started.txt +++ b/docs/getting-started.txt @@ -21,7 +21,7 @@ attribute = None sys.modules['package'] = package = Mock(name='package') - sys.modules['package.module'] = package.module + sys.modules['package.module'] = module = package.module For comprehensive examples, see the unit tests included in the full source @@ -144,6 +144,32 @@ As ``close`` is a mock object is has all the attributes from the previous example. +Mocking Classes +--------------- + +A common use case is to mock out classes instantiated by your code under test. +When you patch a class, then that class is replaced with a mock. Instances +are created by *calling the class*. This means you access the "mock instance" +by looking at the return value of the mocked class. e.g. + +.. doctest:: + + >>> def some_function(): + ... instance = Foo() + ... return instance.method() + ... + >>> with patch(' + +@patch('package.MyClassName') +def test_something(self, my_mock): + instance_mock = my_mock.return_value + instance_mock.my_method.return_value = 'xyz' + +mock autocreates a new mock to be the return value of any mock. So you control the behaviour of the instances of mocked classes by configuring my_mock.return_value. + + + + Naming your mocks ----------------- diff --git a/docs/index.txt b/docs/index.txt index 7169f84..5f21b9a 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -10,7 +10,7 @@ :Date: 2011/03/05 :Homepage: `Mock Homepage`_ :Download: `Mock on PyPI`_ -:Documentation: `PDF Documentation <http://www.voidspace.org.uk/downloads/mock-0.7.1.pdf>`_ +:Documentation: `PDF Documentation <http://www.voidspace.org.uk/downloads/mock-0.7.2.pdf>`_ :License: `BSD License`_ :Support: `Mailing list (testing-in-python@lists.idyll.org) <http://lists.idyll.org/listinfo/testing-in-python>`_ :Issue tracker: `Google code project <http://code.google.com/p/mock/issues/list>`_ diff --git a/mock.py b/mock.py index cb46c58..30177b4 100644 --- a/mock.py +++ b/mock.py @@ -3,7 +3,7 @@ # Copyright (C) 2007-2011 Michael Foord & the mock team # E-mail: fuzzyman AT voidspace DOT org DOT uk -# mock 0.7.1 +# mock 0.7.2 # http://www.voidspace.org.uk/python/mock/ # Released subject to the BSD License @@ -23,7 +23,7 @@ __all__ = ( 'DEFAULT' ) -__version__ = '0.7.2alpha' +__version__ = '0.7.2' __unittest = True @@ -919,6 +919,12 @@ _return_values = { '__index__': 1, } +_side_effect_methods = { + '__eq__': lambda self: lambda other: self is other, + '__ne__': lambda self: lambda other: self is not other, +} + + def _set_return_value(mock, method, name): return_value = DEFAULT @@ -929,6 +935,9 @@ def _set_return_value(mock, method, name): return_value = _calculate_return_value[name](mock) except AttributeError: return_value = AttributeError(name) + elif name in _side_effect_methods: + side_effect = _side_effect_methods[name](mock) + method.side_effect = side_effect if return_value is not DEFAULT: method.return_value = return_value diff --git a/tests/testmagicmethods.py b/tests/testmagicmethods.py index 853c82f..f17b9fe 100644 --- a/tests/testmagicmethods.py +++ b/tests/testmagicmethods.py @@ -179,23 +179,24 @@ class TestMockingMagicMethods(unittest2.TestCase): self. assertTrue(mock >= 3) - def testEquality(self): - mock = Mock() - self.assertEqual(mock, mock) - self.assertNotEqual(mock, Mock()) - self.assertNotEqual(mock, 3) - - def eq(self, other): - return other == 3 - mock.__eq__ = eq - self.assertTrue(mock == 3) - self.assertFalse(mock == 4) - - def ne(self, other): - return other == 3 - mock.__ne__ = ne - self.assertTrue(mock != 3) - self.assertFalse(mock != 4) + def test_equality(self): + for mock in Mock(), MagicMock(): + self.assertEqual(mock == mock, True) + self.assertEqual(mock != mock, False) + self.assertEqual(mock == object(), False) + self.assertEqual(mock != object(), True) + + def eq(self, other): + return other == 3 + mock.__eq__ = eq + self.assertTrue(mock == 3) + self.assertFalse(mock == 4) + + def ne(self, other): + return other == 3 + mock.__ne__ = ne + self.assertTrue(mock != 3) + self.assertFalse(mock != 4) def testLenContainsIter(self): -- GitLab