Merge "mock: Fixes mock.patch.multiple autospec"

This commit is contained in:
Zuul 2018-01-17 10:55:37 +00:00 committed by Gerrit Code Review
commit cccaf73289
2 changed files with 13 additions and 1 deletions

View File

@ -157,7 +157,12 @@ class _patch(mock.mock._patch):
isinstance(target, type))
new = super(_patch, self).__enter__()
_lazy_autospec_method(new, original_attr, eat_self)
# NOTE(claudiub): mock.patch.multiple will cause new to be a
# dict.
mocked_method = (new[self.attribute] if isinstance(new, dict)
else new)
_lazy_autospec_method(mocked_method, original_attr, eat_self)
return new
else:
return super(_patch, self).__enter__()

View File

@ -82,6 +82,13 @@ class MockSanityTestCase(testtools.TestCase):
foo = Foo()
self._check_autospeced_foo(foo)
def test_patch_autospec_multiple(self):
with mock.patch.multiple(Foo, bar=mock.DEFAULT,
classic_bar=mock.DEFAULT,
static_bar=mock.DEFAULT):
foo = Foo()
self._check_autospeced_foo(foo)
@mock.patch.object(Foo, 'static_bar', autospec=False)
@mock.patch.object(Foo, 'classic_bar', autospec=False)
@mock.patch.object(Foo, 'bar', autospec=False)