Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way I can call my local server, but mock the calls my api makes? #191

Closed
joe-narvaez opened this issue Dec 12, 2021 · 2 comments

Comments

@joe-narvaez
Copy link

joe-narvaez commented Dec 12, 2021

Let's say my api will call the pokemon api. Can I mock out the call to the pokemon api indirectly? For example,

from fastapi.testclient import TestClient
client = TestClient(app)

@pytest.mark.respx()
def test_create_team(respx_mock: Any) -> None:
    respx_mock.post(
        "https://graphql-pokemon2.vercel.app/",
        json={"query": f'{{ pokemon(id: "{pika_id}" ){{id name}} }}'},
    ).mock(
        return_value=Response(
            200, json={"data": {"pokemon": {"id": pika_id, "name": "Pikachu"}}}
        )
    )

    query = (
        f'mutation {{ createTeam(pokemonId: "{pika_id}"){{pokemon}} }}'
    )
    client.post("http://localhost:3000/graphql", json={"query": query})

Currently this fails as

 RESPX: <Request('POST', 'https://graphql-pokemon2.vercel.app/')> not mocked!

Is this possible?

@joe-narvaez joe-narvaez changed the title Is there a way I can call my local server, but mock the calls it makes Is there a way I can call my local server, but mock the calls it makes? Dec 12, 2021
@joe-narvaez joe-narvaez changed the title Is there a way I can call my local server, but mock the calls it makes? Is there a way I can call my local server, but mock the calls my api makes? Dec 12, 2021
@lundberg
Copy link
Owner

Need some more input to understand your setup.

Do you have your own rest api, built with fastapi, which in turn calls remotely to graphql-pokemon2.vercel.app using httpx?

@joe-narvaez
Copy link
Author

joe-narvaez commented Dec 13, 2021

I figured it out. With respx you need a testing (fastapi testing client,). I guess it gets closer to the call site and create the patch. With this you can toggle e2e (mocking external apis).

https://docs.python.org/3/library/unittest.mock.html#where-to-patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants