Repository: getsentry/responses
Pull request #360 (2020-11-14 22:19:31)

Improve `replace` error message.

codecov[bot]: # [Codecov](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=h1) Report
> Merging [#360](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=desc) (54552e6) into [master](https://codecov.io/gh/getsentry/responses/commit/30a7fa73e050b72432492e955faead2d37302fb6?el=desc) (30a7fa7) will **decrease** coverage by `0.13%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/getsentry/responses/pull/360/graphs/tree.svg?width=650&height=150&src=pr&token=x2tAGR7rxE)](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #360      +/-   ##
==========================================
- Coverage   96.21%   96.07%   -0.14%     
==========================================
  Files           3        3              
  Lines        1268     1224      -44     
==========================================
- Hits         1220     1176      -44     
  Misses         48       48              
```


| [Impacted Files](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [responses.py](https://app.codecov.io/gh/getsentry/responses/compare/360/diff?src=pr&el=tree#diff-cmVzcG9uc2VzLnB5) | `97.69% <100.00%> (+0.01%)` | :arrow_up: |
| [test\_responses.py](https://app.codecov.io/gh/getsentry/responses/compare/360/diff?src=pr&el=tree#diff-dGVzdF9yZXNwb25zZXMucHk=) | `97.66% <100.00%> (-0.14%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=footer). Last update [30a7fa7...54552e6](https://app.codecov.io/gh/getsentry/responses/compare/360?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/responses.py b/responses.py
index eaf84f3..24d60e1 100644
--- a/responses.py
+++ b/responses.py
@@ -610,11 +610,15 @@ class RequestsMock(object):
         >>> responses.replace(responses.GET, 'http://example.org', json={'data': 2})
         """
         if isinstance(method_or_response, BaseResponse):
+            url = method_or_response.url
             response = method_or_response
         else:
             response = Response(method=method_or_response, url=url, body=body, **kwargs)
 
-        index = self._matches.index(response)
+        try:
+            index = self._matches.index(response)
+        except ValueError:
+            raise ValueError("Response is not registered for URL %s" % url)
         self._matches[index] = response
 
     def add_callback(


Test patch:
diff --git a/test_responses.py b/test_responses.py
index 02089b9..0584a3c 100644
--- a/test_responses.py
+++ b/test_responses.py
@@ -138,8 +138,27 @@ def test_replace_error(original, replacement):
     @responses.activate
     def run():
         responses.add(responses.GET, original)
-        with pytest.raises(ValueError):
+        with pytest.raises(ValueError) as excinfo:
             responses.replace(responses.GET, replacement)
+        assert "Response is not registered for URL %s" % replacement in str(
+            excinfo.value
+        )
+
+    run()
+    assert_reset()
+
+
+def test_replace_response_object_error():
+    @responses.activate
+    def run():
+        responses.add(Response(method=responses.GET, url="http://example.com/one"))
+        with pytest.raises(ValueError) as excinfo:
+            responses.replace(
+                Response(method=responses.GET, url="http://example.com/two")
+            )
+        assert "Response is not registered for URL http://example.com/two" in str(
+            excinfo.value
+        )
 
     run()
     assert_reset()