From 66224aa0856bcb04063a984c003a462deecfd809 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 30 Jan 2018 19:08:42 +0000 Subject: [PATCH] Add a Stub testing helper. --- tests/helpers/stub.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/helpers/stub.py diff --git a/tests/helpers/stub.py b/tests/helpers/stub.py new file mode 100644 index 00000000..c51eff90 --- /dev/null +++ b/tests/helpers/stub.py @@ -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