Skip to content

Instantly share code, notes, and snippets.

@relaxdiego relaxdiego/stuff.py
Last active Aug 29, 2015

Embed
What would you like to do?
class TestSuite:
@patch('real_class_name')
def test_case(self, mock_class):
# Delay the evaluation of Mock() so that we get
# a new mock object for each time the (mocked)
# class constructor is called.
def new_mock_obj(*args, **kwargs):
mock_obj = Mock()
mock_obj.some_function.return_value = 'w00t'
return mock_obj
# If we wrote mock_class.return_value = Mock() here
# then our assertion at the bottom would fail.
mock_class.side_effect = new_mock_obj
mock_objs = []
for i in range(0, 3):
mock_obj = mock_class()
mock_obj.some_function()
mock_objs.add(mock_obj)
for mock_obj in mock_objs:
assert mock_obj.some_function.call_count == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.