The Mock Object
Defining a Mock Object
Take a close look at this code.
Try to predict what will happen.
from unittest.mock import Mock
mock = Mock()
mock.return_value = 3
mock()
mock.assert_called_once_with()
mock()
mock.assert_called_once_with()
Let’s read this code together now:
-
In the line
mock = Mock()
, we are instantiating the mock object. -
In the line
mock.return_value = 3
, we are assigning a return value for this instance of the object. That means that every time I callmock()
(the instance ofMock()
), I’ll get a value of3
. -
Look at
mock.assert_called_once_with()
. Here we are asserting that we called the method with no parameters once. An AssertionError will be raised if this assertion fails. This happens on the second time we callmock.assert_called_once_with()
.
Now you’ve just learned to assert mocked method calls
Mapping method to be mocked by Mock
In development
Mapping exceptions
In development
More on side-effect
In development