Add ResolverMatch object to test client responses.
|Reported by:||mrmachine||Owned by:||nobody|
|Severity:||Normal||Keywords:||test client view url resolve reverse|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The docs say that you can use the test client to "Test that the correct view is executed for a given URL." but this doesn't seem to be possible without requiring users to perform their own resolve(), when Django has already done it and discarded the resulting ResolverMatch object.
response = client.post(some_url, some_data, follow=True) self.assertEqual(resolve(response.request["PATH_INFO"]).url_name, 'some_url')
In my case, some_url is a view that processes some form data and redirects according to the data, and I'm testing that with some_data as input, the request is redirected to some_url. With different data, it might redirect to a different URL.
Initially I tried to use assertRedirects and response.redirect_chain to test this, but ran into trouble because I had to try and construct a URL string that would match the redirect_chain from the view function or URL name, without knowing the exact URL arguments that were used to generate the redirect URL (auto field and a hash that is unique to the newly created object).
I'd like to see Django add the ResolverMatch object directly to all test client responses, so that users can easily test that a specific view function or named URL was executed.
I guess this would be implemented similarly to the way the context and templates are added.
I think this is either a feature bug or a documentation bug. If this is not a valid feature of the test client, the docs should be clarified. I read "correct view is executed" to mean "correct view function (or class based view) is executed".
Change History (9)
comment:1 Changed 3 years ago by aaugustin
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Design decision needed
comment:3 Changed 3 years ago by aaugustin
- Triage Stage changed from Design decision needed to Accepted
comment:4 Changed 17 months ago by mrmachine
- Resolution set to fixed
- Status changed from new to closed
comment:5 Changed 17 months ago by mrmachine
- Resolution fixed deleted
- Status changed from closed to reopened