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__.
This commit is contained in:
Carlton Gibson 2022-01-26 20:58:22 +01:00 committed by GitHub
parent f38c3cbadc
commit d15a10afb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View file

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