How do you mock a function which has decorator apply to it in a unit test?

mocking, python, unit-testing

Solution

My first thought would be to create a `_lolanimal` method that encapsulates all the actual functionality of `lolanimal` and then just make `lolanimal` a pass through wrapper around `_lolanimal`. Then you could just run all your tests against `_lolanimal` with data you fully control.

You also might be able to create a second decorator that would come before the first that would read a config value or something for some sort of testing mode that would override the `lolspecific` decorator if the config value is true...

Problem

``` @lolcat_decorator1 @loldog_decorator2 @lolrat_decorator3 def lolanimal(*args, **kwargs): .... ``` I am sure I will unit-test those decorators separately. But these decorators will do stuff to the parameters passed to `lolanimal` first, and then `lolanimal` will do stuff to those modified parameters (one of those decorators may insert new keyword arguments to `**kwargs`.) So what's the best way to mock it? Thanks

Original source