diff --git a/CHANGES b/CHANGES index 6e1bcf1c..cd8cc233 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,7 @@ since they are not actual properties of the class instance. Use `responses.mock.assert_all_requests_are_fired`, `responses.mock.passthru_prefixes`, `responses.mock.target` instead. +* Fixed the issue when `reset()` method was called in not stopped mock. See #511 0.19.0 ------ diff --git a/responses/__init__.py b/responses/__init__.py index 91ee3ea4..d8d5438a 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -560,7 +560,6 @@ def reset(self): self._registry = FirstMatchRegistry() self._calls.reset() self.passthru_prefixes = () - self._patcher = None def add( self, @@ -834,6 +833,8 @@ def _on_request(self, adapter, request, **kwargs): def start(self): if self._patcher: # we must not override value of the _patcher if already applied + # this prevents issues when one decorated function is called from + # another decorated function return def unbound_on_send(adapter, request, *a, **kwargs): @@ -847,6 +848,10 @@ def stop(self, allow_assert=True): # prevent stopping unstarted patchers self._patcher.stop() + # once patcher is stopped, clean it. This is required to create a new + # fresh patcher on self.start() + self._patcher = None + if not self.assert_all_requests_are_fired: return diff --git a/responses/tests/test_responses.py b/responses/tests/test_responses.py index 9f2212c2..05e816e2 100644 --- a/responses/tests/test_responses.py +++ b/responses/tests/test_responses.py @@ -2127,3 +2127,16 @@ def run(): run() assert_reset() + + +def test_reset_in_the_middle(): + @responses.activate + def run(): + with responses.RequestsMock() as rsps2: + rsps2.reset() + responses.add(responses.GET, "https://example.invalid", status=200) + resp = requests.request("GET", "https://example.invalid") + assert resp.status_code == 200 + + run() + assert_reset()