Commit d33f72e
committed
Mock async iterators properly
By default AsyncMock doesn't provide any proper mocking of async
async iterators, and if we just assign a `AsyncMock` to stub calls like
`ReceivePublicTradesStream`, what happens is that
`async for x in ReceivePublicTradesStream()` is used instead. What
Python does with this is something like:
```py
stream = ReceivePublicTradesStream()
it = stream.__aiter__() # Gets an anync iterator, but this call is sync
while v := await it.__anext():
...
```
Because `ReceivePublicTradesStream` is an `AsyncMock`, getting any
attributes (like `__aiter__()`) results in returning a new `AsyncMock`.
But since `__aiter__()` is called without an `await`, then we get the
following exceptions/warnings:
* TypeError("'async for' requires an object with __aiter__ method, got
coroutine")
* RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was
never awaited
To fix this, we just crate a proper fake async iterator we can return
to the async mock.
Signed-off-by: Leandro Lucarella <[email protected]>1 parent 1e0170f commit d33f72e
1 file changed
+26
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
39 | 49 | | |
40 | 50 | | |
41 | 51 | | |
| |||
133 | 143 | | |
134 | 144 | | |
135 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
136 | 150 | | |
137 | 151 | | |
138 | 152 | | |
| |||
143 | 157 | | |
144 | 158 | | |
145 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
146 | 164 | | |
147 | 165 | | |
148 | 166 | | |
| |||
162 | 180 | | |
163 | 181 | | |
164 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
165 | 187 | | |
166 | 188 | | |
167 | 189 | | |
| |||
177 | 199 | | |
178 | 200 | | |
179 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
180 | 206 | | |
181 | 207 | | |
182 | 208 | | |
| |||
0 commit comments