Home:ALL Converter>Mock a decorator function to bypass decorator logic

Mock a decorator function to bypass decorator logic

Ask Time:2020-10-01T20:13:05         Author:Jerry

Json Formatter

I'm trying write some unittests for my code which in turn uses a decorator

import unittest
from unittest.mock import patch
from functools import wraps

def decorator(f):
    @wraps(f)
    def decorated(x):
        return f(x+1)
    return decorated

@decorator
def get_value(x):
    return x
    
class MyTestCase(unittest.TestCase):
    @patch('file.decorator', lambda f: f)
    def test_something(self):
        result = get_value(1)
        self.assertEqual(1, result)

I'm trying to mock the decorated function to just return f and completely bypass the decorator logic part. This is talked about in Overriding decorator during unit test in python but doesn't really work.

Author:Jerry,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64155169/mock-a-decorator-function-to-bypass-decorator-logic
yy