mercredi 6 juin 2018

Mocking: wrapping method and receiving args at call time

I wonder wether it's possible to patch a method somewhere deep inside code I don't own in a way that allows me to receive the args the method will receive right before or after that method is called without having an actual reference to the object on which the method is being called.

I want to be able to do something like

def wrapping(method, *args, **kwargs):
    # do stuff
    rv = method(*args, **kwargs)
    # more stuff
    return rv


with mock.patch('path.to.The.method', wraps=wrapping):
    code_that_eventually_calls_the_method()

and then I get dropped into wrapping when the method is eventually called, but I can call the mocked method without having a reference to its object beforehand in the testcode. Is this achievable?

Aucun commentaire:

Enregistrer un commentaire