Merge lp:~allenap/maas/patch-mock-thing into lp:maas/trunk

Proposed by Gavin Panella on 2012-08-24
Status: Merged
Approved by: Gavin Panella on 2012-08-24
Approved revision: 927
Merged at revision: 926
Proposed branch: lp:~allenap/maas/patch-mock-thing
Merge into: lp:maas/trunk
Diff against target: 58 lines (+28/-0)
2 files modified
src/maastesting/testcase.py (+17/-0)
src/maastesting/tests/test_testcase.py (+11/-0)
To merge this branch: bzr merge lp:~allenap/maas/patch-mock-thing
Reviewer Review Type Date Requested Status
Jeroen T. Vermeulen (community) 2012-08-24 Approve on 2012-08-24
Review via email: mp+121210@code.launchpad.net

Commit Message

Patch-in a MagicMock when no value is specified to TestCase.patch().

To post a comment you must log in.
Jeroen T. Vermeulen (jtv) wrote :

Makes sense.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maastesting/testcase.py'
2--- src/maastesting/testcase.py 2012-08-24 02:51:42 +0000
3+++ src/maastesting/testcase.py 2012-08-24 15:59:19 +0000
4@@ -20,6 +20,7 @@
5 from fixtures import TempDir
6 from maastesting.factory import factory
7 from maastesting.scenarios import WithScenarios
8+import mock
9 from nose.proxy import ResultProxy
10 from nose.tools import nottest
11 from provisioningserver.testing.worker_cache import WorkerCacheFixture
12@@ -121,3 +122,19 @@
13 def __call__(self, result=None):
14 with active_test(result, self):
15 super(TestCase, self).__call__(result)
16+
17+ def patch(self, obj, attribute, value=mock.sentinel.unset):
18+ """Patch `obj.attribute` with `value`.
19+
20+ If `value` is unspecified, a new `MagicMock` will be created and
21+ patched-in instead.
22+
23+ This is a thin customisation of `testtools.TestCase.patch`, so refer
24+ to that in case of doubt.
25+
26+ :return: The patched-in object.
27+ """
28+ if value is mock.sentinel.unset:
29+ value = mock.MagicMock()
30+ super(TestCase, self).patch(obj, attribute, value)
31+ return value
32
33=== modified file 'src/maastesting/tests/test_testcase.py'
34--- src/maastesting/tests/test_testcase.py 2012-04-20 11:04:42 +0000
35+++ src/maastesting/tests/test_testcase.py 2012-08-24 15:59:19 +0000
36@@ -16,7 +16,9 @@
37 from shutil import rmtree
38 from tempfile import mkdtemp
39
40+from maastesting.factory import factory
41 from maastesting.testcase import TestCase
42+from mock import MagicMock
43 from testtools.matchers import (
44 DirExists,
45 FileExists,
46@@ -48,3 +50,12 @@
47 self.patch(self, 'make_dir', lambda: directory)
48 dir_part, file_part = os.path.split(self.make_file())
49 self.assertEqual(directory, dir_part)
50+
51+ def test_patch_can_mock(self):
52+ # The patch method patches-in and returns a new MagicMock() instance
53+ # if no attribute value is given.
54+ attribute_name = factory.make_name("attribute")
55+ self.assertRaises(AttributeError, getattr, self, attribute_name)
56+ attribute = self.patch(self, attribute_name)
57+ self.assertIs(getattr(self, attribute_name), attribute)
58+ self.assertIsInstance(attribute, MagicMock)