Commit 8c79dec
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 d236cd5 commit 8c79dec
1 file changed
+25
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
39 | 49 | | |
40 | 50 | | |
41 | 51 | | |
| |||
125 | 135 | | |
126 | 136 | | |
127 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
128 | 142 | | |
129 | 143 | | |
130 | 144 | | |
| |||
135 | 149 | | |
136 | 150 | | |
137 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
138 | 156 | | |
139 | 157 | | |
140 | 158 | | |
| |||
153 | 171 | | |
154 | 172 | | |
155 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
156 | 178 | | |
157 | 179 | | |
158 | 180 | | |
| |||
168 | 190 | | |
169 | 191 | | |
170 | 192 | | |
| 193 | + | |
| 194 | + | |
171 | 195 | | |
172 | 196 | | |
173 | 197 | | |
| |||
0 commit comments