Merge "mock: Apply autospec to a mock's return_value"
This commit is contained in:
commit
974666550e
|
@ -44,13 +44,19 @@ class _AutospecMockMixin(object):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(_AutospecMockMixin, self).__init__(*args, **kwargs)
|
super(_AutospecMockMixin, self).__init__(*args, **kwargs)
|
||||||
self.__dict__['_autospec'] = kwargs.get('autospec')
|
autospec = kwargs.get('autospec')
|
||||||
|
self.__dict__['_autospec'] = autospec
|
||||||
_mock_methods = self.__dict__['_mock_methods']
|
_mock_methods = self.__dict__['_mock_methods']
|
||||||
if _mock_methods:
|
if _mock_methods:
|
||||||
# this will allow us to be able to set _mock_check_sig if
|
# this will allow us to be able to set _mock_check_sig if
|
||||||
# the spec_set argument has been given.
|
# the spec_set argument has been given.
|
||||||
_mock_methods.append('_mock_check_sig')
|
_mock_methods.append('_mock_check_sig')
|
||||||
|
|
||||||
|
# callable mocks with autospecs (e.g.: the given autospec is a class)
|
||||||
|
# should have their return values autospeced as well.
|
||||||
|
if autospec:
|
||||||
|
self.return_value.__dict__['_autospec'] = autospec
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
attr = super(_AutospecMockMixin, self).__getattr__(name)
|
attr = super(_AutospecMockMixin, self).__getattr__(name)
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,7 @@ class MockSanityTestCase(testtools.TestCase):
|
||||||
for spec in [Foo, Foo()]:
|
for spec in [Foo, Foo()]:
|
||||||
foo = mock_cls(autospec=spec)
|
foo = mock_cls(autospec=spec)
|
||||||
self._check_autospeced_foo(foo)
|
self._check_autospeced_foo(foo)
|
||||||
|
self._check_autospeced_foo(foo())
|
||||||
|
|
||||||
def test_mock_autospec_all_members(self):
|
def test_mock_autospec_all_members(self):
|
||||||
self._check_mock_autospec_all_members(mock.Mock)
|
self._check_mock_autospec_all_members(mock.Mock)
|
||||||
|
|
Loading…
Reference in New Issue