Is monkeypatching a coroutine possible? #10439
-
I have a function within my database handler that is an async function written for a FastAPI endpoint. It simply pulls the latest document from a MongoDB instance. I'm trying to provide a monkeypatched response from this function where it returns an example document:
Where 'MongoDb' is the name of the database handler class, and it is properly imported into the test file. 'System_document' is the fixture I wish to have returned. The problem lies in how it is called in the endpoint I am testing:
Because it's called with await it's expecting a callable coroutine that it then waits for the response from. It ends up giving this error:
Which is clearly because await is trying to call the async coroutine but is instead acting on the dictionary object I've provided as a mock. Is there a better way to go about mocking a response for something like this? There is probably room for debate about whether or not this particular function should be async anyway, but I'd quite like a solution with this setup that allows me to PyTest the endpoint in isolation with mocked responses. I've tried looking for solutions on the Web and haven't found much, nor much in the PyTest discussions or issues. I may just not be understanding monkeypatching fully. Any suggestions or solutions are appreciated. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Well, put in a async function instead of a sync one? If you expect a awaitable of dict, return that instead of a dict |
Beta Was this translation helpful? Give feedback.
Well, put in a async function instead of a sync one?
If you expect a awaitable of dict, return that instead of a dict