patch.object() as Decorator
00:00
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 @patch.object()
.
00:11
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 Timeout
.
00:25
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,
00:42
and then we’ll bump this line in one indentation and we’ll say, let’s see.requests.get()
, side_effect
is Timeout
—yeah, so I think we don’t need to do anything there.
00:55
We don’t need to add any more code, we’ve already mocked .get()
, we’ve set the side_effect
to Timeout
, and we’re asserting that it raises a Timeout
.
01:03 So let’s go ahead and run the test once more.
01:07
It succeeds—no 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.