django - 测试 Jinja2 支持的 Django View 时如何访问 response.context

标签 django testing jinja2 django-testing

当我使用 Django test.client 时,我会做类似的事情:

class MyTestCase(TestCase):
    def test_this(self):
        c = self.client
        response = c.get('/')
        assert False, response.context['name']

我得到一个错误:

assert False, response.context['name']
TypeError: 'NoneType' object is unsubscriptable

我唯一的猜测是使用 Jinja2 会阻止上下文在我测试时出现。

请注意,此测试是有意设置为失败的。

最佳答案

我一直想阅读TestCase。仔细阅读文档后,您可能会遇到错误。断言是 TestCase 类的方法。

class MyTestCase(TestCase):
  def test_this(self):
    response=self.client.get('/')
    self.assertEquals(response.context['name'],'Jim') 

关于django - 测试 Jinja2 支持的 Django View 时如何访问 response.context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1941980/

相关文章:

python - 在 django 模型中加载树结构时出现问题

python - GridFS (MongoDB) 的自定义存储系统?

exception - 在 SML 中,如何断言特定异常被抛出?

Ansible 模板给出错误 "Could not find or access template file "

django - 如何在我的 models.py 中引用 Django 设置变量?

django - 使用 webpackDevServer 对 Django 进行 Docker 化

windows - 如何使用 Windows 容器进行软件测试?

c# - 通过 exe 文件运行 c# 测试项目

python - 更新 html 中的文本

flask - 如何使用 Jinja 呈现链接的完整 URL(用于电子邮件)