M
M
maxclax2015-03-24 22:18:14
Django
maxclax, 2015-03-24 22:18:14

What is the gap in the tests?

Testing templatetags with the following code:

from django.test import TestCase
from django.template import Template, Context
from django.shortcuts import render_to_response
from news.models import News


class NewsTemplatetagsTest(TestCase):
    TEMPLATE = Template("{% load news %} {% last %}")

    def test_show_last(self):
        html = render_to_response('news/last.html', {'data': News.objects.get_list(limit=3)})

        rendered = self.TEMPLATE.render(Context({}))

        self.assertEquals(html.content.decode('utf8'), rendered)

Self templatetags
from django import template
from news.models import News

register = template.Library()


@register.inclusion_tag('news/last.html', takes_context=True)
def last(context):
    return {'data': News.objects.get_list(limit=3)}

as a result, there is a space at the beginning of the rendered content, even if you make the template file empty.

self.assertEquals(html.content.decode('utf8'), rendered)
AssertionError: '[<News: title_49>, <News: title_48>, <News: title_47>]' != ' [<News: title_49>, <News: title_48>, <News: title_47>]'


self.assertEquals(html.content.decode('utf8'), rendered)
AssertionError: '' != ' '

Tell me, where is the joint? Maybe I'm not doing the test right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2015-03-25
@maxclax

"{% load news %} {% last %}"
You have a space between "{% load news %}" and "{% last %}"
PS Your test is kind of meaningless, because. the tag code and the test code do the same thing. It's like testing assert foo() == foo(). Well, yes, this assert is unlikely to ever break, even if you delete the entire function code, but what is the point is not clear. It is highly desirable that the test does not know anything about the implementation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question