[TIP] Can you mock a function that has a decorator applies to it?

John Wong gokoproject at gmail.com
Wed Jul 4 11:06:33 PDT 2012


Okay... I guess I would have to add a line that references the undecorated
reference like this?

def simple_decorator(f):
    def decorated(*args, **kwargs):
        print 'I am a decorator...'
        print 'I am exiting decorator'
    decorated._original = f
    return decorated

@simple_decorator
def aFunction():
    print "aFunction running"

if __name__ == '__main__':
    aFunction()
    aFunction._original()



On Wed, Jul 4, 2012 at 1:50 PM, Julian Berman <julian at grayvines.com> wrote:

> I'd probably expect you to mock out whatever the decorator is calling out
> to instead.
>
> Barring that there's not really a general way to retrieve the original
> function object off the decorated function without poking at its locals. At
> that point you'd probably be better off just storing an undecorated
> reference in your code to make it more testable.
>
>
> On Wednesday, July 4, 2012, John Wong wrote:
>
>> There are times when decorator really shine and I don't see a way to Mock
>> out the decorator. I know it replaces the current function.... is there a
>> way out of this?
>>
>> Thanks.
>>
>> John
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120704/75db520d/attachment.htm>


More information about the testing-in-python mailing list