def patch(
self, obj, attribute=None, value=mock.sentinel.unset) -> MagicMock:
"""Patch `obj.attribute` with `value`.
If `value` is unspecified, a new `MagicMock` will be created and
patched-in instead. Its ``__name__`` attribute will be set to
`attribute` or the ``__name__`` of the replaced object if `attribute`
is not given.
This is a thin customisation of `testtools.TestCase.patch`, so refer
to that in case of doubt.
:return: The patched-in object.
"""
# If 'attribute' is None, assume 'obj' is a 'fully-qualified' object,
# and assume that its __module__ is what we want to patch. For more
# complex use cases, the two-parameter 'patch' will still need to
# be used.
if attribute is None:
attribute = obj.__name__
obj = import_module(obj.__module__)
if value is mock.sentinel.unset:
value = MagicMock(__name__=attribute)
super(MAASTestCase, self).patch(obj, attribute, value)
return value
评论列表
文章目录