respx icon indicating copy to clipboard operation
respx copied to clipboard

Enhance iterable side effect experience

Open lundberg opened this issue 11 months ago • 0 comments

As discussed in #271, it's quite annoying that StopIteration is raised when an iterable side effect is exhausted.

In most cases, it's probably more reasonable to have the last entry repeated, which the docs has been adjusted (#287).

Since current behaviour is by design, to align with pythons already familiar built-in unittest Mock behaviour, we should find a way to opt-in for the new logic, e.g. by ..

.. providing a repeat_last utility to shortcut chain + repeat ..

respx.get("https://example.org/").mock(
    side_effect=repeat_last(
        httpx.Response(201),
        httpx.Response(200),
    )
)

.. or alter the api by adding an optional kwarg to route.mock(..) ..

respx.get("https://example.org/").mock(
    side_effect=[
        httpx.Response(201),
        httpx.Response(200),
    ],
    repeat_last=True,
)

I'm leaning towards the utility shortcut, as a first step.

lundberg avatar Jan 20 '25 15:01 lundberg