Allow reusing settings from a MockRouter instance on MockRouter.__call__
See original GitHub issueI’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:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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()
onRouteList
,Route
andPattern
(nested) to not mutate parent router state 😅 .