嗨我想嘲笑我的装饰师因为我不想实际调用/执行这个功能。但我似乎无法找到下面的解决方案是我的代码
# This is the decorator located in my project
# This is located in custom.mydecorators.decorator_file.custom_decorator
Base = declarative_base()
def custom_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("This message is still printed even when I try to patch this function")
try:
#code here
my_var = CoolClass()
retval = func(*args, **kwargs)
except Exception as e:
#rollback code here
raise e
return retval
return wrapper
现在我尝试使用此代码修补此内容
patch('custom.mydecorators.decorator_file.custom_decorator', lambda x: x).start()
class TestMockDecoratorsCallingClass(unittest.TestCase):
def test_should_return_success_if_decorators_are_mocked(self):
# Code here
我的装饰器在非单元测试文件中正常工作。但是,如果我嘲笑这个装饰器,它就不能说局部变量' my_var'在分配前引用
注意:my_var在装饰器内部我试图模拟/修补打印消息仍然执行即使我尝试修补它