mercredi 8 avril 2015

How to test that a Mock object has been called?

Here is the simple method that I want to test:



class InputManager(object):
''' Responsible for managing user input. '''

def __init__(self, path_table):
self._path_table = path_table

def setInput(self, position, input):
''' Edits the input in table at table[position]. '''
self._path_table.setInput(position, input)


And here is the test:



from mock import Mock
def test_set_input(self):
''' Test that set input calls correctly. '''
position = 1
input = 'G'
fake_path = Mock()
iman = InputManager(fake_path())
fake_path.return_value.setInput.assert_called_with(position, input)


It always fails, with this assertion error message:



_mock_self = <Mock name='mock().setInput' id='4418507856'>, args = (1, 'G'), kwargs = {}
self = <Mock name='mock().setInput' id='4418507856'>, expected = "setInput(1, 'G')"

def assert_called_with(_mock_self, *args, **kwargs):
"""assert that the mock was called with the specified arguments.

Raises an AssertionError if the args and keyword args passed in are
different to the last call to the mock."""
self = _mock_self
if self.call_args is None:
expected = self._format_mock_call_signature(args, kwargs)
> raise AssertionError('Expected call: %s\nNot called' % (expected,))
E AssertionError: Expected call: setInput(1, 'G')
E Not called

/Library/Python/2.7/site-packages/mock.py:831: AssertionError


I have read the mock documentation, however, I am still not sure how to test that self._path_table.setInput(position, input) is being called correctly.


Aucun commentaire:

Enregistrer un commentaire