Side Effects as an Iterable (Part 2)
00:00 Okay, so it appears that you may only be able to use the arguments and the return value of a function once. That’s kind of my hypothesis. So what I want to try is to change the order of the iterable.
We’re going to first assert the response and then assert that it raises the
Timeout, and it should have still been called twice. Let’s clear the screen, run our tests again, and—that still didn’t work.
So I’m going to undo what I did, because that wasn’t the problem. The problem, I think, is that when you set
.side_effect as an iterable, it gets reset each time you call it. So when we call
get_holidays(), we’re calling
requests.get(), and maybe the
.side_effect just gets wiped out.
01:32 And then after we call the function, the first time we’ll print out the side effects again, and then let’s save that and run our program again, see if it printed out anything. Okay, so that’s not really helpful. Um, hm.
take the logic of our
get_holidays() function—we can take the logic of
get_holidays() and bring it into this test itself. If we were to go in here and take the
response_mock and bring it into our test function, once we have this
response_mock we could say the side effects are going to be
Timeout and then
So that way we’re only going to be calling
requests.get() once, and then the
.side_effect iterable, this
list_iterator, is not going to get reset. And let me re-fix the typo, and let’s go ahead and clear the screen and try this again.
Each time you call the function that you have set the
.side_effect to, the iterable will get reset. So you either have to take the logic out of the function you’re calling and bring it into your tests, or you have to reset the iterable each time.
Become a Member to join the conversation.