Python 如何使用mock存根方法装饰器?

Python 如何使用mock存根方法装饰器?,python,unit-testing,mocking,stub,Python,Unit Testing,Mocking,Stub,如何使用python来剔除方法装饰器的行为,从而不依赖它们来测试当前方法 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

如何使用python来剔除方法装饰器的行为,从而不依赖它们来测试当前方法

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')

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

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

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