getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.17k stars 355 forks source link

response.calls is not registering my api calls #732

Closed risky-rickman closed 4 months ago

risky-rickman commented 4 months ago

Describe the bug

I am able to successfully mock my api call, however when i check the responses.calls iterable, it is empty.

Additional context

No response

Version of responses

0.25.03

Steps to Reproduce

@responses.activate
def test():
    ...
    # Monkey patch refresh api call
    os.environ["CACHE_REFRESH_API_URL"] = "http://mock"
    responses.add(
        responses.POST, os.environ["CACHE_REFRESH_API_URL"], status=200, json={"a": "b"}
    )
    # run the main entrypoint, which makes the mocked api call
    main()
    #This errors, because the iterable is empty
    assert responses.calls[0]

Expected Result

I should be able to inspect the details of the call with responses.calls[1]

Actual Result

the responses.calls iterable is empty

markstory commented 4 months ago

I am unable to reproduce your issue locally. However, I don't know what happens inside of main() could you provide a smaller example that I could run locally to reproduce the problem you're seeing?

risky-rickman commented 4 months ago

I was able to isolate the issue a bit more. It looks like the responses library has issues working with the aws mock library.

import responses
import os
import moto
import requests

@responses.activate
def test():
    # AWS mock start
    moto_fake = moto.mock_aws()
    moto_fake.start()
    # Monkey patch refresh api call
    responses.add(responses.POST, "http://mock", status=200, json={"a": "b"})
    response = requests.request(
        "POST",
        "http://mock",
        data={"projectId": 1},
    )
    assert len(responses.calls) == 1

test()
beliaev-maksim commented 4 months ago

moto has its own nuances. Especially, how to combine it with responses.

They did a great job documenting it, please see the link: https://docs.getmoto.org/en/latest/docs/faq.html#how-can-i-mock-my-own-http-requests-using-the-responses-module

risky-rickman commented 4 months ago

Thanks for the info! I'll give this a try and close out the ticket if it works.

risky-rickman commented 4 months ago

Works like a charm!