Fixed #29758 -- Documented how to test custom error views.

This commit is contained in:
Cammil Taank 2018-09-19 11:57:34 +01:00 committed by Tim Graham
parent b3b47bf515
commit f83a689f61
1 changed files with 36 additions and 0 deletions

View File

@ -166,3 +166,39 @@ The :func:`~django.views.defaults.bad_request` view is overridden by
Use the :setting:`CSRF_FAILURE_VIEW` setting to override the CSRF error
view.
Testing custom error views
--------------------------
To test the response of a custom error handler, raise the appropriate exception
in a test view. For example::
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.test import SimpleTestCase, override_settings
from django.urls import path
def response_error_handler(request, exception=None):
return HttpResponse('Error handler content', status=403)
def permission_denied_view(request):
raise PermissionDenied
urlpatterns = [
path('403/', permission_denied_view),
]
handler403 = response_error_handler
# ROOT_URLCONF must specify the module that contains handler403 = ...
@override_settings(ROOT_URLCONF=__name__)
class CustomErrorHandlerTests(SimpleTestCase):
def test_handler_renders_template_response(self):
response = self.client.get('/403/')
# Make assertions on the response here. For example:
self.assertContains(response, 'Error handler content', status_code=403)