Configure Mocks (Part 2)
One more quick thing about configuring your mocks. Here’s another example. In our previous code, we had this mocked response from our
get_holidays() API that had two holidays in it. In reality, your responses from an API may have thousands, tens of thousands, hundreds of thousands of key-value pairs in it.
When you configure your
Mock object, obviously you don’t want to one-by-one type out every single key-value pair. Usually you’re going to have that stored somewhere in memory, in a variable like this.
holidays is the same exact dictionary as what we had typed in our mock response. So if you have access to a variable that contains the data that you want to mock, you can configure your mock to use that instead of hand typing it yourself.
So instead of creating a
response_mock as just a generic
Mock object and then defining its
.json.return_value, what you could do is say
response_mock is a
Mock object, and then use this little syntax here—this double star (
**)—to unpack a dictionary of
'json.return_value', and then this key will have the value of
Become a Member to join the conversation.