gpt4 book ai didi

python - 如何使用模拟来 stub 方法装饰器?

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:35 24 4
gpt4 key购买 nike

如何使用 python mock消除方法装饰器的行为,以便我不依赖它们来测试当前方法?

import utils
class Router(object):
@utils.with_user
@utils.formatted_response('resources', with_pagination=True)
def get_resources(self, user_id=None, offset=None, limit=None):
# do stuff
pass

我已经尝试过:

# @patch('utils.with_tenant')
# @patch.object(utils, 'with_tenant')
def test_stub_decorator(self):
# patch('utils.with_tenant')
# patch.object(utils, 'with_tenant')

这些似乎都不起作用!有什么想法吗?

最佳答案

对于模拟装饰器,您需要提前模拟它们(在加载该模块之前):

from mock import patch
# mock the retry decorator before any module loads it
patch('utils.with_user', lambda x: x).start()
import utils # or any module which imports utils

关于python - 如何使用模拟来 stub 方法装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17752862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com