Is there a way I can call my local server, but mock the calls my api makes?
joe-narvaez opened this issue · 2 comments
joe-narvaez commented
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?
lundberg commented
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 commented
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