Created
August 19, 2014 13:48
-
-
Save steinnes/7db16e4478acfd7223d6 to your computer and use it in GitHub Desktop.
mockmethod.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I couldn't figure out how to do this with Mock or MagicMock | |
class MockMethod(object): | |
def __init__(self, *return_values): | |
self.return_values = return_values | |
self.called = 0 | |
self.call_args = [] | |
def __call__(self, *args, **kwargs): | |
self.called += 1 | |
self.call_args.append([args, kwargs]) | |
return self.return_value | |
@property | |
def return_value(self): | |
ret = self.return_values[0] | |
if len(self.return_values) > 1: | |
self.return_values = self.return_values[1:] | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This allows the method being mocked to return potentially different values with subsequent calls. Interesting for mocking user-input, for example when validating or prompting repeatedly and needing different return values.