Configure Mocks (Part 2)
00:00
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.
00:20
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.
00:32
So, 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.
00:50
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 holidays
.
01:20
So now we can say response_mock.json()
and it returns that dictionary.
01:29
That’s just a really nice tool to have actual data inside your Mock
object without having to copy and paste it or type it by hand.
Become a Member to join the conversation.
gabrielyassunaga on Dec. 26, 2022
isn’t this the same as using this?