Add fixture for mock.patch.multiple
Add another fixture to support mock.patch.multiple() so that test suites using that function don't have to import mock directly. Change-Id: I58368dddda347410459d8f5a713e7395253c3697
This commit is contained in:
		@@ -15,6 +15,7 @@ oslotest.mockpatch
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
.. automodule:: oslotest.mockpatch
 | 
					.. automodule:: oslotest.mockpatch
 | 
				
			||||||
   :members:
 | 
					   :members:
 | 
				
			||||||
 | 
					   :special-members:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
oslotest.moxstubout
 | 
					oslotest.moxstubout
 | 
				
			||||||
===================
 | 
					===================
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,6 @@ class PatchObject(fixtures.Fixture):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Patch(fixtures.Fixture):
 | 
					class Patch(fixtures.Fixture):
 | 
				
			||||||
 | 
					 | 
				
			||||||
    """Deal with code around mock.patch."""
 | 
					    """Deal with code around mock.patch."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, obj, new=mock.DEFAULT, **kwargs):
 | 
					    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)
 | 
					        _p = mock.patch(self.obj, self.new, **self.kwargs)
 | 
				
			||||||
        self.mock = _p.start()
 | 
					        self.mock = _p.start()
 | 
				
			||||||
        self.addCleanup(_p.stop)
 | 
					        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)
 | 
					        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):
 | 
					class TestMockPatchObject(base.BaseTestCase):
 | 
				
			||||||
    def test_mock_patch_object_with_replacement(self):
 | 
					    def test_mock_patch_object_with_replacement(self):
 | 
				
			||||||
        self.useFixture(mockpatch.PatchObject(Foo, 'bar', mocking_bar))
 | 
					        self.useFixture(mockpatch.PatchObject(Foo, 'bar', mocking_bar))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user