lundberg/respx

Incomplete `AllMockedAssertionError` description

lxmnk opened this issue · 2 comments

lxmnk commented

Hello! Thank you for this useful library!

I've found that AllMockedAssertionError description is incomplete. For example, JSON in this snippet will fail the test:

import httpx
import pytest


@pytest.mark.asyncio
async def test_reproduce(respx_mock):
    respx_mock.post("https://foo.bar/", json={"foo": "bar"}).mock(
        return_value=httpx.Response(201)
    )
    async with httpx.AsyncClient() as client:
        response = await client.post("https://foo.bar/", json={"baz": "quux"})

    assert response.status_code == 201

But the error doesn't contain any information about JSON difference:

self = <respx.router.MockRouter object at 0x7f5a2778e860>
request = <Request('POST', 'https://foo.bar/')>

    @contextmanager
    def resolver(self, request: httpx.Request) -> Generator[ResolvedRoute, None, None]:
        resolved = ResolvedRoute()
    
        try:
            yield resolved
    
            if resolved.route is None:
                # Assert we always get a route match, if check is enabled
                if self._assert_all_mocked:
>                   raise AllMockedAssertionError(f"RESPX: {request!r} not mocked!")
E                   respx.models.AllMockedAssertionError: RESPX: <Request('POST', 'https://foo.bar/')> not mocked!

.venv/lib/python3.10/site-packages/respx/router.py:251: AllMockedAssertionError

Good point @lxmnk ! The assertion errors should include more details 👍 .

Unfortunately the context does not have any more details. The assertion check is for when no respx mock route was found, i.e. we only have the request object, and the error message contains the repr of the httpx.Request instance, so IMO nothing more we could/should do. Feels a bit too much if we json decode request body etc for error messages.

Closing this for now, but please re-open if you have any ideas @lxmnk. PR's welcome 😉 .