Skip to content

Instantly share code, notes, and snippets.

@relaxdiego
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relaxdiego/9801728 to your computer and use it in GitHub Desktop.
Save relaxdiego/9801728 to your computer and use it in GitHub Desktop.
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