question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow reusing settings from a MockRouter instance on MockRouter.__call__

See original GitHub issue

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__.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lundbergcommented, Dec 2, 2021

Side note…this would be of interest even for #190 in the case a route is added to the default global respx router, then all subsequent routers would inherit that/those added routes and a test case would only need to be decorated with the sub-router.

0reactions
lundbergcommented, Nov 12, 2021

But if I’m interpreting you right here, the problematic part is that bases are merged with each route and we have no way of “rewinding” that merge? So that we can only edit the bases patterns.

Correct @flaeppe.

At the moment, I’m trying to actually implement a rewind feature, or more correctly adjust the merge_patterns function to smartly override any existing bases with new ones in a pattern tree.

Also to get that working, I had to implement .clone() on RouteList, Route and Pattern (nested) to not mutate parent router state 😅 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · lundberg/respx - GitHub
Consider a warning about missing router arg. #197 opened ; Allow reusing settings from a MockRouter instance on MockRouter.__call__ enhancement New feature or ......
Read more >
User Guide - RESPX
Isolated routers are useful when mocking multiple remote APIs, allowing grouped routes per API, and to be mocked individually or stacked for reuse...
Read more >
Jest reuse mock next/route object - Stack Overflow
Try this: Create a file mock in the folder: <rootDir>/jest/__mocks__/routerMock.js and add mock jest.mock('next/router', ...
Read more >
How to mock Next router with Jest - DEV Community ‍ ‍
To demonstrate how to mock next/router I will use an example. ... used radio inputs so I could mock router.push in testing this...
Read more >
Testing Vue Router
Instead of using a real Vue Router instance, we can create a mock version which ... const mockRouter = { push: jest.fn() }...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found