【发布时间】:2014-03-16 07:33:54
【问题描述】:
我正在尝试编写一个测试,测试在基于类的视图ListView 传递的上下文中是否存在object_list。
目前我有代码:
views.py
class BlogView(ListView):
model = Post
template_name = 'core/blog.html'
model.py:
class Post(models.Model):
title = models.CharField(max_length=500)
content = models.TextField()
tests.py
class BlogListViewTests(TestCase):
def setUp(self):
self.resp = self.client.get('/blog/')
def test_has_a_post_list(self):
self.assertContains(u'post_list', self.resp.context)
当我运行测试时出现以下错误:
Traceback (most recent call last):
File "/home/breno/projects/blog/core/tests.py", line 36, in test_has_a_post_list
self.assertContains(self.resp.context, 'post_list')
File "/home/breno/.virtualenvs/blog/local/lib/python2.7/site-packages/django/test/testcases.py", line 325, in assertContains
self.assertEqual(response.status_code, status_code,
AttributeError: 'ContextList' object has no attribute 'status_code'
打印self.resp.context 我看到一个带有u'post_list' 键的Tube:
[[{'False': False, 'None': None, 'True': True}, {u'paginator': None, u'post_list': [], u'object_list': [],...,]]
我的测试出了什么问题?我怎样才能做这个测试?类型
【问题讨论】:
-
错误不是来自那个断言(因为你有 AttributeError,而不是 AssertionError)。请发布完整的回溯。
标签: python django unit-testing testing tdd