Skip to content

Instantly share code, notes, and snippets.

@romuald
Created September 2, 2019 12:18
Show Gist options
  • Save romuald/01e2d60fcb736acf8a48c24c963cba2b to your computer and use it in GitHub Desktop.
Save romuald/01e2d60fcb736acf8a48c24c963cba2b to your computer and use it in GitHub Desktop.
A simple pass-though mock template
import unittest.mock
def mock_passthrough(*args, **kwargs):
"""
A simple mock template to check that a method was called,
not modifying to original patched method.
Behave the same as mock.patch.object(), default autospec is True,
and side_effect is always overrided
Usage:
>>> patch = mock_passthrough(object, 'attribute')
... with patch as mocked:
... do_stuff()
... mocked.assert_called()
"""
if 'autospec' not in kwargs:
kwargs['autospec'] = True
def side_effect(*sargs, **kwsargs):
return patch.temp_original(*sargs, **kwsargs)
kwargs['side_effect'] = side_effect
patch = unittest.mock.patch.object(*args, **kwargs)
return patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment