mock: Properly patch mock.MagicMock

MockAutospecFixture should patch both internal and external usages
of MagicMock (mock.MagicMock and mock.mock.MagicMock). However, only
the internal one is patched properly.

Related-Bug: #1735588

Change-Id: Ib9709f1cf5dbed4792f5dd7c49d8f9c77f04419f
This commit is contained in:
Claudiu Belu 2018-03-28 01:07:18 -07:00
parent 9460095d7d
commit bb78b84c3f
2 changed files with 9 additions and 3 deletions

View File

@ -106,7 +106,7 @@ class MockAutospecFixture(fixtures.Fixture):
# patch both external and internal usage of Mock / MagicMock.
self.useFixture(fixtures.MonkeyPatch('mock.Mock', _AutospecMock))
self.useFixture(fixtures.MonkeyPatch('mock.mock.Mock', _AutospecMock))
self.useFixture(fixtures.MonkeyPatch('mock.mock.MagicMock',
self.useFixture(fixtures.MonkeyPatch('mock.MagicMock',
_AutospecMagicMock))
self.useFixture(fixtures.MonkeyPatch('mock.mock.MagicMock',
_AutospecMagicMock))

View File

@ -70,11 +70,17 @@ class MockSanityTestCase(testtools.TestCase):
# assert that AttributeError is raised if the method does not exist.
self.assertRaises(AttributeError, getattr, foo, 'lish')
def test_mock_autospec_all_members(self):
def _check_mock_autospec_all_members(self, mock_cls):
for spec in [Foo, Foo()]:
foo = mock.Mock(autospec=spec)
foo = mock_cls(autospec=spec)
self._check_autospeced_foo(foo)
def test_mock_autospec_all_members(self):
self._check_mock_autospec_all_members(mock.Mock)
def test_magic_mock_autospec_all_members(self):
self._check_mock_autospec_all_members(mock.MagicMock)
@mock.patch.object(Foo, 'static_bar')
@mock.patch.object(Foo, 'classic_bar')
@mock.patch.object(Foo, 'bar')