Created
October 6, 2015 01:42
-
-
Save poros/a9ef76ebcd86fff295eb to your computer and use it in GitHub Desktop.
Mock side effect
This file contains hidden or 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
# raise exception | |
mock = Mock(side_effect=KeyError('foo')) | |
mock() | |
# KeyError: 'foo' | |
# return value based on argument | |
values = {'a': 1, 'b': 2, 'c': 3} | |
def side_effect(arg): | |
return values[arg] | |
mock.side_effect = side_effect | |
mock('a'), mock('b'), mock('c') | |
(1, 2, 3) | |
# return value based on numbers of calls | |
mock.side_effect = [5, 4, 3, 2, 1] | |
mock(), mock(), mock() | |
(5, 4, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment