Skip to content

Commit 57d8c29

Browse files
authored
Update migrate.md with requests-mock examples (#301)
1 parent 06091fa commit 57d8c29

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

docs/migrate.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,62 @@ respx.route(url="https://example.org/").pass_through()
174174

175175
## requests-mock
176176

177-
*todo ... contribution welcome* ;-)
177+
### Decorator
178+
179+
```python
180+
@requests_mock.mock()
181+
def test_some_call(self, m: requests_mock.mock):
182+
m.get(requests_mock.ANY, json={})
183+
```
184+
```python
185+
@respx.mock()
186+
def test_some_call(self, respx_mock: respx.Router):
187+
respx_mock.get().respond(json={})
188+
189+
```
190+
191+
### Context manager
192+
193+
```python
194+
with requests_mock.mock() as m:
195+
m.get(requests_mock.ANY, json=json)
196+
```
197+
```python
198+
with respx.mock() as respx_mock:
199+
respx_mock.get().respond(json=json)
200+
```
201+
202+
### Raising an exception
203+
204+
```python
205+
m.post(requests_mock.ANY, exc=JSONDecodeError("nope", "ok", 1))
206+
```
207+
```python
208+
respx.post().side_effect = JSONDecodeError("nope", "ok", 1)
209+
```
210+
211+
### Specifying a list of responses
212+
213+
```python
214+
m.get(requests_mock.ANY, responses)
215+
```
216+
```python
217+
respx.get().side_effect = responses
218+
```
219+
220+
### Assertions
221+
222+
```python
223+
self.assertTrue(m.called_once)
224+
self.assertEqual(m.last_request.url, "https://api.io/example/endpoint")
225+
self.assertEqual(m.last_request.json(), {"key": "value"})
226+
```
227+
228+
```python
229+
respx.calls.assert_called_once()
230+
self.assertEqual(
231+
str(respx.calls.last.request.url),
232+
"https://api.io/example/endpoint",
233+
)
234+
self.assertEqual(json.loads(respx.calls.last.request.content), {"key": "value"})
235+
```

0 commit comments

Comments
 (0)