Skip to content

Allow reusing settings from a MockRouter instance on MockRouter.__call__ #181

@flaeppe

Description

@flaeppe

I'm wondering if it could be possible to support reusing some settings of a MockRouter instance, when invoking __call__. Basically that'll allow me to specify a MockRouter with defaults, and for specific test cases flip the assert_all_called-flag.

Consider the code below. I want base_url and assert_all_called to have its default, unless they're overriden by a new __call__ invocation

import httpx
import respx
default = respx.mock(base_url="http://some.where/", assert_all_called=False)
default.get(path="/here/")

with default(assert_all_called=True):
    httpx.get("http://some.where/here/")  # Raises: RESPX: <Request('GET', 'http://some.where/here/')> not mocked!

@default(assert_all_called=True)
def test_1():
    httpx.get("http://some.where/here/")  # Raises: RESPX: <Request('GET', 'http://some.where/here/')> not mocked!

test_1()

# This should still have the default `assert_all_called=False`
@default
def test_2():
    httpx.get("http://some.where/here/")  # This works

test_2()

Currently, this gives me an error whenever I pass in new kwargs to default(test_1 and with default). As __call__ is just creating a new MockRouter instance from scratch.

I'm realising as I write this that support for this might appear better through some additional MockRouter method (e.g. MockRouter.replace()) instead of changing __call__.

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