Last active
August 29, 2015 13:57
-
-
Save relaxdiego/9801728 to your computer and use it in GitHub Desktop.
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
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