requests_mock.mock() fails to nest
Bug #1642690 reported by
Marius Gedminas
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
requests-mock |
Fix Released
|
Medium
|
Jamie Lennox |
Bug Description
Here's a test case you can run with py.test:
def test_nested_
import requests_mock, requests
with requests_
m1.get('/', json=lambda *a: {'m': 1})
assert requests.
with requests_
assert requests.
In my actual test suite the nesting is hidden by a mixture of pytest fixtures (some function-scoped, some session-scoped) and it took me a while to discover what was happening.
To post a comment you must log in.
A patch like this should fix it:
diff --git a/requests_ mock/mocker. py b/requests_ mock/mocker. py mock/mocker. py mock/mocker. py
index bb9e1e5..36326ed 100644
--- a/requests_
+++ b/requests_
@@ -26,6 +26,9 @@ POST = 'POST'
PUT = 'PUT'
+original_send = requests. Session. send
+
+
class MockerCore(object):
"""A wrapper around common mocking functions.
@@ -86,7 +89,7 @@ class MockerCore(object):
requests. Session. get_adapter = _fake_get_adapter
try: send(session, request, **kwargs) send(session, request, **kwargs) NoMockAddress:
raise
- return self._real_
+ return original_
except exceptions.
if not self._real_http: