#24622: Response "context" and "templates" not available in the Test Client when
using Jinja2 - Django 1.8
-------------------------------------+------------------------------------
     Reporter:  rivantis             |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Testing framework    |                  Version:  1.8
     Severity:  Normal               |               Resolution:
     Keywords:  jinja2, test client  |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+------------------------------------

Comment (by Roy Smith):

 FWIW, here's the workaround I've been using.  It monkey-patches
 django.shortcuts.render to send the required signal (part of
 [https://github.com/roysmith/spi-
 tools/blob/5ed0cd8f2285698f31f3355b9afa55f64f19bbab/spi/test_views.py this
 source file]).  The downside is it doesn't get you the full template
 chain, just the top-level one you call directly from your view.  For my
 purposes, that's (mostly) good enough, but it's certainly not a plug-
 compatible replacement.

 I say "mostly" because I'm now looking at a case where I need to see
 included templates.  Sadly, I think this approach is too complicated to
 extend to support that, so I'm probably going to fall back to a low-tech
 solution: having each of my jinja templates emit a HTML comment confessing
 the template name and parse the generated HTML looking for those.

 Django 3.1.12
 Jinja 2.11.3
 Python 3.7.3


 {{{
 from unittest.mock import patch

 from django.test import TestCase
 from django.test.signals import template_rendered
 from django.shortcuts import render


 class ViewTestCase(TestCase):
     """Base class for all SPI view tests.

     Subclass this and have setUp() call super().setUp('spi.my_view')
     for a view defined in my_view.py.

     """
     @staticmethod
     def render_patch(request, template, context):
         """Work-around for the django test client not working properly
 with
         jinga2 templates (https://code.djangoproject.com/ticket/24622).

         """
         template_rendered.send(sender=None, template=template,
 context=context)
         return render(request, template, context)


     def setUp(self, view_module_name):
         render_patcher = patch(f'{view_module_name}.render',
 autospec=True)
         self.mock_render = render_patcher.start()
         self.mock_render.side_effect = self.render_patch
         self.addCleanup(render_patcher.stop)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/24622#comment:28>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.f24f5367a0b4a331408f7b3bbe43aebd%40djangoproject.com.

Reply via email to