Skip to content

Instantly share code, notes, and snippets.

@toast38coza
Created October 29, 2014 11:50
Show Gist options
  • Save toast38coza/352f5f535fb6efd2eb4e to your computer and use it in GitHub Desktop.
Save toast38coza/352f5f535fb6efd2eb4e to your computer and use it in GitHub Desktop.
A strategy for mocking consecutive calls. For example, if you need to test retrying something if you get an Exception on your first pass
@patch.object(TaskHelper, "get_parent")
def test_mocking(self, mock_get_parent):
responses = [Exception('boom'), 'response']
mock_get_parent.side_effect = lambda *args, **kw: responses.pop(0)
result = TaskHelper.get_parent()
if isinstance(result, Exception):
raise result
result = TaskHelper.get_parent()
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment