Parche __call__ de una función
Necesito parchar la fecha y hora actual en las pruebas. Estoy usando esta solución:
def _utcnow():
return datetime.datetime.utcnow()
def utcnow():
"""A proxy which can be patched in tests.
"""
# another level of indirection, because some modules import utcnow
return _utcnow()
Luego, en mis pruebas, hago algo como:
with mock.patch('***.utils._utcnow', return_value=***):
...
Pero hoy se me ocurrió una idea, que podría simplificar la implementación parcheando__call__
de funciónutcnow
en lugar de tener un adicional_utcnow
.
Esto no funciona para mi:
from ***.utils import utcnow
with mock.patch.object(utcnow, '__call__', return_value=***):
...
¿Cómo hacer esto con elegancia?