Created
March 26, 2013 18:24
-
-
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...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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