Skip to content

Instantly share code, notes, and snippets.

@truemped
Created November 17, 2012 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save truemped/4094605 to your computer and use it in GitHub Desktop.
Save truemped/4094605 to your computer and use it in GitHub Desktop.
Tornado Asynchronous Mock
from mock import CallableMixin, NonCallableMock
class AsyncCallableMixin(CallableMixin):
"""Change the __call__ method such that it does not return but call the
`callback` kwarg with the return value.
"""
def __call__(_mock_self, *args, **kwargs):
cb = kwargs.get('callback', None)
_mock_self._mock_check_sig(*args, **kwargs)
if cb:
del kwargs['callback']
result = _mock_self._mock_call(*args, **kwargs)
cb(result)
else:
return _mock_self._mock_call(*args, **kwargs)
class AsyncMock(AsyncCallableMixin, NonCallableMock):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment