Skip to content

Instantly share code, notes, and snippets.

@arruda
Created March 26, 2013 18:24
Show Gist options
  • Save arruda/5247839 to your computer and use it in GitHub Desktop.
Save arruda/5247839 to your computer and use it in GitHub Desktop.
Usar RequestContext no Lugar de Context em testes onde for necessário renderizar um template manualmente. Se não usar o RequestContext ele ignora alguns, senão todos os TEMPLATE_CONTEXT_PROCESSORS, e não renderiza como deveria ou dependendo da template em questão, ele nem roda...
# -*- coding: utf-8 -*-
from lxml import html as lhtml
from django.template import Context, Template, RequestContext
from django.test import RequestFactory
factory = RequestFactory()
class TestTemplate(AlgumTestCase):
request = factory.get('/')
def setUp(self):
super(TestTemplate, self).setUp()
self.template = Template(
'{% extends "meu_template.html" %}'
)
self.html_string = self.template.render(
RequestContext(self.request,{
'algo': 'alguma coisa',
})
)
self.html = lhtml.fromstring(self.html_string)
def test_deveria_ter_algo(self):
algo = self.html.cssselect('.algo')
assert algo
# -*- coding: utf-8 -*-
from lxml import html as lhtml
from django.template import Context, Template
class TestTemplate(AlgumTestCase):
def setUp(self):
super(TestTemplate, self).setUp()
self.template = Template(
'{% extends "meu_template.html" %}'
)
self.html_string = self.template.render(
Context({
'algo': 'alguma coisa',
})
)
self.html = lhtml.fromstring(self.html_string)
def test_deveria_ter_algo(self):
algo = self.html.cssselect('.algo')
assert algo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment