testcase.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:maas 作者: maas 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号