diff options
author | Jacob R. Rothenbuhler <jakerothenbuhler@gmail.com> | 2014-04-14 15:13:49 -0400 |
---|---|---|
committer | Simon Charette <charette.s@gmail.com> | 2014-04-14 15:55:10 -0400 |
commit | 17c18844561431aabed89c3bd48de951db7d13ab (patch) | |
tree | e706e99fda9117d12e89cfe66c262dd4b323ba83 /tests/test_client_regress | |
parent | 09af48c70fb5cc652ea109487015472e9ef984df (diff) | |
download | django-17c18844561431aabed89c3bd48de951db7d13ab.tar.gz |
Fixed #22369 -- Added count parameter to assertTemplateUsed
Diffstat (limited to 'tests/test_client_regress')
-rw-r--r-- | tests/test_client_regress/tests.py | 34 | ||||
-rw-r--r-- | tests/test_client_regress/urls.py | 1 | ||||
-rw-r--r-- | tests/test_client_regress/views.py | 7 |
3 files changed, 42 insertions, 0 deletions
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py index 59cc46b567..da903dfcfd 100644 --- a/tests/test_client_regress/tests.py +++ b/tests/test_client_regress/tests.py @@ -213,6 +213,12 @@ class AssertTemplateUsedTests(TestCase): except AssertionError as e: self.assertIn("abc: No templates used to render the response", str(e)) + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed(response, 'GET Template', count=2) + self.assertIn( + "No templates used to render the response", + str(context.exception)) + def test_single_context(self): "Template assertions work when there is a single context" response = self.client.get('/post_view/', {}) @@ -237,6 +243,21 @@ class AssertTemplateUsedTests(TestCase): except AssertionError as e: self.assertIn("abc: Template 'Empty POST Template' was not a template used to render the response. Actual template(s) used: Empty GET Template", str(e)) + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed(response, 'Empty GET Template', count=2) + self.assertIn( + "Template 'Empty GET Template' was expected to be rendered 2 " + "time(s) but was actually rendered 1 time(s).", + str(context.exception)) + + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed( + response, 'Empty GET Template', msg_prefix='abc', count=2) + self.assertIn( + "abc: Template 'Empty GET Template' was expected to be rendered 2 " + "time(s) but was actually rendered 1 time(s).", + str(context.exception)) + def test_multiple_context(self): "Template assertions work when there are multiple contexts" post_data = { @@ -263,6 +284,19 @@ class AssertTemplateUsedTests(TestCase): except AssertionError as e: self.assertIn("Template 'Valid POST Template' was not a template used to render the response. Actual template(s) used: form_view.html, base.html", str(e)) + with self.assertRaises(AssertionError) as context: + self.assertTemplateUsed(response, 'base.html', count=2) + self.assertIn( + "Template 'base.html' was expected to be rendered 2 " + "time(s) but was actually rendered 1 time(s).", + str(context.exception)) + + def test_template_rendered_multiple_times(self): + """Template assertions work when a template is rendered multiple times.""" + response = self.client.get('/render_template_multiple_times/') + + self.assertTemplateUsed(response, 'base.html', count=2) + @override_settings(ROOT_URLCONF='test_client_regress.urls') class AssertRedirectsTests(TestCase): diff --git a/tests/test_client_regress/urls.py b/tests/test_client_regress/urls.py index 44bbebfcd8..1bd0e0bc5b 100644 --- a/tests/test_client_regress/urls.py +++ b/tests/test_client_regress/urls.py @@ -37,4 +37,5 @@ urlpatterns = [ url(r'^read_all/$', views.read_all), url(r'^read_buffer/$', views.read_buffer), url(r'^request_context_view/$', views.request_context_view), + url(r'^render_template_multiple_times/$', views.render_template_multiple_times), ] diff --git a/tests/test_client_regress/views.py b/tests/test_client_regress/views.py index db5de3bf87..656d7a4651 100644 --- a/tests/test_client_regress/views.py +++ b/tests/test_client_regress/views.py @@ -7,6 +7,7 @@ from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response from django.core.serializers.json import DjangoJSONEncoder from django.template import RequestContext +from django.template.loader import render_to_string from django.test import Client from django.test.client import CONTENT_TYPE_RE from django.test.utils import setup_test_environment @@ -151,3 +152,9 @@ def request_context_view(request): # Special attribute that won't be present on a plain HttpRequest request.special_path = request.path return render_to_response('request_context.html', context_instance=RequestContext(request, {})) + + +def render_template_multiple_times(request): + """A view that renders a template multiple times.""" + return HttpResponse( + render_to_string('base.html') + render_to_string('base.html')) |