Skip to content

Instantly share code, notes, and snippets.

@zmsmith
Created April 4, 2012 23:37
Show Gist options
  • Save zmsmith/2306590 to your computer and use it in GitHub Desktop.
Save zmsmith/2306590 to your computer and use it in GitHub Desktop.
Method for creating a Mock side_effect that only raises an exception for n calls
def get_exception_side_effect(num_of_exceptions, exception_class=Exception):
def side_effect(*args, **kwargs):
side_effect.calls += 1
if side_effect.calls > num_of_exceptions:
return Mock()
else:
raise exception_class
side_effect.calls = 0
return side_effect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment