Ticket #15826: 15826.diff

File 15826.diff, 3.1 KB (added by mmcnickle, 13 years ago)

Render TemplateReponse inside assertContains

  • django/test/testcases.py

     
    1515    reset_queries)
    1616from django.http import QueryDict
    1717from django.test import _doctest as doctest
     18from django.template.response import SimpleTemplateResponse
    1819from django.test.client import Client
    1920from django.test.utils import get_warnings_state, restore_warnings_state, override_settings
    2021from django.utils import simplejson, unittest as ut2
     
    415416        If ``count`` is None, the count doesn't matter - the assertion is true
    416417        if the text occurs at least once in the response.
    417418        """
     419
     420        if isinstance(response, SimpleTemplateResponse):
     421            response.render()
     422
    418423        if msg_prefix:
    419424            msg_prefix += ": "
    420425
     
    438443        successfully, (i.e., the HTTP status code was as expected), and that
    439444        ``text`` doesn't occurs in the content of the response.
    440445        """
     446
     447        if isinstance(response, SimpleTemplateResponse):
     448            response.render()
     449
    441450        if msg_prefix:
    442451            msg_prefix += ": "
    443452
  • tests/regressiontests/testcase/tests.py

     
     1from django.test import TestCase
     2
     3from django.http import HttpResponse
     4from django.template.response import SimpleTemplateResponse
     5from django.template import Template
     6
     7class TestCaseTests(TestCase):
     8
     9    def test_assert_contains_renders_template_response(self):
     10        """ Test that we can pass in an unrendered SimpleTemplateReponse
     11            without throwing an error.
     12        """
     13        response = SimpleTemplateResponse(Template('Hello'), status=200)
     14        self.assertContains(response, 'Hello')
     15
     16    def test_assert_contains_using_non_template_response(self):
     17        """ Test that auto-rendering does not affect responses that aren't
     18            instances (or subclasses) of SimpleTemplateResponse.
     19        """
     20        response = HttpResponse('Hello')
     21        self.assertContains(response, 'Hello')
     22
     23    def test_assert_not_contains_renders_template_response(self):
     24        """ Test that we can pass in an unrendered SimpleTemplateReponse
     25            without throwing an error.
     26        """
     27        response = SimpleTemplateResponse(Template('Hello'), status=200)
     28        self.assertNotContains(response, 'Bye')
     29
     30    def test_assert_not_contains_using_non_template_response(self):
     31        """ Test that auto-rendering does not affect responses that aren't
     32            instances (or subclasses) of SimpleTemplateResponse.
     33        """
     34        response = HttpResponse('Hello')
     35        self.assertNotContains(response, 'Bye')
Back to Top