5

我正在为一个应用程序编写一套测试。

在许多地方,我使用 Django 方便的“assertTemplateUsed”。这适用于所有模板,除了我看到的通过模板标签加载的模板(作为包含标签对象)。

当通过模板标签加载模板时,是否有另一种方法可以使用断言模板?我当然可以检查模板中的字符串,但检查模板的使用会是一个更好的解决方案。

4

1 回答 1

2

尝试捕获在测试期间可用的template_rendered信号。

从文档:

django.test.signals.template_rendered

Sent when the test system renders a template. This signal is not emitted during normal operation of a Django server – it is only available during testing.

Arguments sent with this signal:

sender
    The Template object which was rendered.
template
    Same as sender
context
    The Context with which the template was rendered. 
于 2013-04-29T09:42:23.147 回答