Home:ALL Converter>Mock return value of decorator on method under test

Mock return value of decorator on method under test

Ask Time:2018-02-20T19:21:00         Author:ediblecode

Json Formatter

As the title says, I'm having a little trouble trying to check that my function under test is decorated with the correct decorator for one, and secondly the right parameters.

Method under test

@command_wrapper.command('string1', 'string2')
def function_under_test():
    return some_other_function();

Unit test

@patch("command_wrapper.command")
def test_function_under_test(self, mock_command_wrapper)
    * Do some testing *

Decorator: command_wrapper.py

def command(string1, string2):
    cmd_decorator(func):
        @wraps(func)
        def func_wrapper():
            * Do some code *
    return func_wrapper
return cmd_decorator

However when the test runs, its clearly executing the decorator without having mocked it. I'm new to Python so I'm not even sure if this is possible, or whether this is the right approach.

Author:ediblecode,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/48884155/mock-return-value-of-decorator-on-method-under-test
yy