summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlton Gibson <carlton@noumenal.es>2022-01-26 20:58:22 +0100
committerGitHub <noreply@github.com>2022-01-26 20:58:22 +0100
commitd15a10afb51619faf14e678deae7dcda720413d9 (patch)
tree7b5287d37ab30f5d28ee91f9a52c2f5486cf4397
parentf38c3cbadcb2b1bf77057cdbe89b06ca7bb71016 (diff)
Adjusted CBV resolver_match example in testing tools docs.
The view_class is available on the view callback, allowing that to be checked, rather than the __name__.
-rw-r--r--docs/topics/testing/tools.txt8
-rw-r--r--tests/test_client/tests.py9
2 files changed, 13 insertions, 4 deletions
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt
index 1d8cbfbd72..c6923c6c87 100644
--- a/docs/topics/testing/tools.txt
+++ b/docs/topics/testing/tools.txt
@@ -541,12 +541,12 @@ Specifically, a ``Response`` object has the following attributes:
You can use the :attr:`~django.urls.ResolverMatch.func` attribute, for
example, to verify the view that served the response::
- # my_view here is a function based view
+ # my_view here is a function based view.
self.assertEqual(response.resolver_match.func, my_view)
- # class-based views need to be compared by name, as the functions
- # generated by as_view() won't be equal
- self.assertEqual(response.resolver_match.func.__name__, MyView.as_view().__name__)
+ # Class-based views need to compare the view_class, as the
+ # functions generated by as_view() won't be equal.
+ self.assertIs(response.resolver_match.func.view_class, MyView)
If the given URL is not found, accessing this attribute will raise a
:exc:`~django.urls.Resolver404` exception.
diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py
index f138cc69b3..2496a1d79a 100644
--- a/tests/test_client/tests.py
+++ b/tests/test_client/tests.py
@@ -32,6 +32,7 @@ from django.test import (
)
from django.urls import reverse_lazy
from django.utils.decorators import async_only_middleware
+from django.views.generic import RedirectView
from .views import TwoArgException, get_view, post_view, trace_view
@@ -213,6 +214,14 @@ class ClientTest(TestCase):
response = self.client.get('/get_view/')
self.assertEqual(response.resolver_match.url_name, 'get_view')
+ def test_response_resolver_match_class_based_view(self):
+ """
+ The response ResolverMatch instance can be used to access the CBV view
+ class.
+ """
+ response = self.client.get('/accounts/')
+ self.assertIs(response.resolver_match.func.view_class, RedirectView)
+
@modify_settings(MIDDLEWARE={'prepend': 'test_client.tests.middleware_urlconf'})
def test_response_resolver_match_middleware_urlconf(self):
response = self.client.get('/middleware_urlconf_view/')