From bb78b84c3fc8485e590f2c925c07731a9740a852 Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Wed, 28 Mar 2018 01:07:18 -0700 Subject: [PATCH] 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 --- oslotest/mock_fixture.py | 2 +- oslotest/tests/unit/test_mock_fixture.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/oslotest/mock_fixture.py b/oslotest/mock_fixture.py index fb9d008..ba53dd8 100644 --- a/oslotest/mock_fixture.py +++ b/oslotest/mock_fixture.py @@ -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)) diff --git a/oslotest/tests/unit/test_mock_fixture.py b/oslotest/tests/unit/test_mock_fixture.py index 73afe8b..c5175b7 100644 --- a/oslotest/tests/unit/test_mock_fixture.py +++ b/oslotest/tests/unit/test_mock_fixture.py @@ -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')