Merge "Add fixture for mock.patch.multiple"
This commit is contained in:
commit
bbfde1e29c
@ -15,6 +15,7 @@ oslotest.mockpatch
|
||||
|
||||
.. automodule:: oslotest.mockpatch
|
||||
:members:
|
||||
:special-members:
|
||||
|
||||
oslotest.moxstubout
|
||||
===================
|
||||
|
@ -36,7 +36,6 @@ class PatchObject(fixtures.Fixture):
|
||||
|
||||
|
||||
class Patch(fixtures.Fixture):
|
||||
|
||||
"""Deal with code around mock.patch."""
|
||||
|
||||
def __init__(self, obj, new=mock.DEFAULT, **kwargs):
|
||||
@ -49,3 +48,33 @@ class Patch(fixtures.Fixture):
|
||||
_p = mock.patch(self.obj, self.new, **self.kwargs)
|
||||
self.mock = _p.start()
|
||||
self.addCleanup(_p.stop)
|
||||
|
||||
|
||||
class Multiple(fixtures.Fixture):
|
||||
"""Deal with code around mock.patch.multiple."""
|
||||
|
||||
# Default value to trigger a MagicMock to be created for a named
|
||||
# attribute.
|
||||
DEFAULT = mock.DEFAULT
|
||||
|
||||
def __init__(self, obj, **kwargs):
|
||||
"""Initialize the mocks
|
||||
|
||||
Pass name=value to replace obj.name with value.
|
||||
|
||||
Pass name=Multiple.DEFAULT to replace obj.name with a
|
||||
MagicMock instance.
|
||||
|
||||
:param obj: Object or name containing values being mocked.
|
||||
:type obj: str or object
|
||||
:param kwargs: names and values of attributes of obj to be mocked.
|
||||
|
||||
"""
|
||||
self.obj = obj
|
||||
self.kwargs = kwargs
|
||||
|
||||
def setUp(self):
|
||||
super(Multiple, self).setUp()
|
||||
_p = mock.patch.multiple(self.obj, **self.kwargs)
|
||||
self.mock = _p.start()
|
||||
self.addCleanup(_p.stop)
|
||||
|
@ -41,6 +41,20 @@ class TestMockPatch(base.BaseTestCase):
|
||||
self.assertIsInstance(instance.bar(), mock.MagicMock)
|
||||
|
||||
|
||||
class TestMockMultiple(base.BaseTestCase):
|
||||
def test_mock_multiple_with_replacement(self):
|
||||
self.useFixture(mockpatch.Multiple('%s.Foo' % (__name__),
|
||||
bar=mocking_bar))
|
||||
instance = Foo()
|
||||
self.assertEqual(instance.bar(), 'mocked!')
|
||||
|
||||
def test_mock_patch_without_replacement(self):
|
||||
self.useFixture(mockpatch.Multiple('%s.Foo' % (__name__),
|
||||
bar=mockpatch.Multiple.DEFAULT))
|
||||
instance = Foo()
|
||||
self.assertIsInstance(instance.bar(), mock.MagicMock)
|
||||
|
||||
|
||||
class TestMockPatchObject(base.BaseTestCase):
|
||||
def test_mock_patch_object_with_replacement(self):
|
||||
self.useFixture(mockpatch.PatchObject(Foo, 'bar', mocking_bar))
|
||||
|
Loading…
x
Reference in New Issue
Block a user