Configure Mocks (Part 1)
What we’ve done so far is something like
mock = Mock() and then
mock.side_effect is maybe a
KeyError exception. And we’ve kind of configured our
Mock attributes line by line, like
.call_count = 0 and
mock.return_value = True.
And then we can use our
Mock object and see how it raises a
KeyError because we’ve set the
.side_effect. Rather than setting these attributes of the
Mock object line by line, we can set them when we first initiate the
Mock object by using the
.__init__() method that comes with this
I’ll clear the screen and show you what I mean. Instead of doing it line by line, we can just say
mock = Mock() and then within the parentheses, we can define our
side_effect, or whatever we want to do. Let’s say the
side_effect is an
and this is useful for when you want to change an attribute of a
Mock object that already exists. So for example, when we call
mock(), it raises an
AttributeError—but what if we didn’t want that anymore, or what if we wanted to change that?
Become a Member to join the conversation.