本文介绍了Django单元测试响应上下文为无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么在以下测试中没有response.context?我已经检查了response.content,它是''。如果我删除assertIsNotNone行,我得到一个TypeError:'NoneType'对象不可订阅
Why is response.context None in the following tests? I've checked response.content and it is ''. if I remove the assertIsNotNone line I get a TypeError: 'NoneType' object is not subscribable
def test_log_view_with_no_entries(self):
"""
If no logs are available, an appropriate message should be displayed.
"""
response = self.client.get(reverse('swpgr:logs'))
self.assertIsNotNone(response.context)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No logs are available.")
self.assertQuerysetEqual(response.context['log_list'], [])
def test_log_view_with_one_entry(self):
"""
Logs should be displayed it table.
"""
time = timezone.now()
t = LogEntry.objects.create(event_date=time, command_type="Test", command_source="Test Case",
username="user")
response = self.client.get(reverse('swpgr:logs'))
self.assertIsNotNone(response.context)
self.assertQuerysetEqual(
response.context['log_list'],
['<LogEntry: 1 Test user: user>']
)`
推荐答案
我遇到这个问题,并修复它在setUp函数中执行以下操作:
I was having this issue and to fix it did the following in the setUp function:
from django.test.utils import setup_test_environment
setup_test_environment()
这篇关于Django单元测试响应上下文为无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!