Side Effects: Advanced Test (Part 2)
We want to make a
GET request to our API, so we’re going to say
requests.get.side_effect, and here instead of passing an exception or something, we’re going to pass this
So one tricky thing here is that when we set the
.side_effect to a function, it’s going to inherit its arguments and its return value. So in other words, when we say
requests.get(), remember that originally, the arguments of
requests.get() is going to be this URL.
And I know it’s a little confusing, but it’s just something that you have to know and kind of roll with, is that
requests.get()—which we’ve set up here in our
get_holidays() function—has this argument passed in into it, so to speak.
So that means we can do something like
assert get_holidays()—so we call that function—and this should now have a response. So we’re mocking the request and we have the
200, so that means that
get_holidays() is going to drop into this block and return
.json(), which we have mocked here—we’ve mocked the
.return_value as this dictionary.
Let’s see if this actually works now. I’m going to open up a terminal and we will run our tests. Since we have this
unittest.main(), it should pick up these three tests that start with
test_. In the previous video, we already saw that these should pass, so let’s go ahead and cross our fingers and see if our new test passes.
Become a Member to join the conversation.