patch.object() as Decorator
As I mentioned, you can use
patch.object() as a decorator as well, so let me just show you what that looks like. We’ll decorate our function with
The first argument is the exact same way here as we’ve done, so
requests and then we’re mocking the
.get() method, setting the
side_effect to a
And this time we need to pass this
mocked_get as an argument. This is an arbitrary name—you can call it whatever you want, but I think
mocked_get makes sense.
00:36 And then we’ll remove this context manager syntax,
and then we’ll bump this line in one indentation and we’ll say, let’s see.
Timeout—yeah, so I think we don’t need to do anything there.
We don’t need to add any more code, we’ve already mocked
.get(), we’ve set the
Timeout, and we’re asserting that it raises a
01:03 So let’s go ahead and run the test once more.
SyntaxErrors. And that just shows that you can use
patch.object() as a decorator, as well as a context manager.
Become a Member to join the conversation.