0

Hi I would like to mock my decorator since I don't want to be actually calling/executing this function. But I can't seem to find the solution for this below are my code

# 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

Now I'm trying to patch this using this code

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 decorators work properly in a non unittest file. But if I mock this decorator it fails saying that the local variable 'my_var' referenced before assignment

Note: my_var is inside the decorator I'm trying to mock/patch also the print message is still executed even when I try to patch it

MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76

0 Answers0