Add a Stub testing helper.

This commit is contained in:
Eric Snow 2018-01-30 19:08:42 +00:00
parent 2fc452e3d8
commit 66224aa085

25
tests/helpers/stub.py Normal file
View file

@ -0,0 +1,25 @@
class Stub(object):
"""A testing double that tracks calls."""
def __init__(self):
self.calls = []
self._exceptions = []
def set_exceptions(self, *exceptions):
self._exceptions = list(exceptions)
def add_call(self, name, *args, **kwargs):
self.add_call_exact(name, args, kwargs)
def add_call_exact(self, name, args, kwargs):
self.calls.append((name, args, kwargs))
def maybe_raise(self):
if not self._exceptions:
return
exc = self._exceptions.pop(0)
if exc is None:
return
raise exc