Skip to content

Enhance iterable side effect experience #288

@lundberg

Description

@lundberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions