gpt4 book ai didi

python - 如何测试为 CBV ListView 生成的上下文中是否存在 'object_list'

转载 作者:行者123 更新时间:2023-11-28 21:05:04 25 4
gpt4 key购买 nike

我正在尝试编写一个测试,测试在基于类的 View ListView 传递的上下文中是否存在 object_list

目前我有代码:

View .py

    class BlogView(ListView):
model = Post
template_name = 'core/blog.html'

模型.py:

    class Post(models.Model):
title = models.CharField(max_length=500)
content = models.TextField()

测试.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' 的 Tuble:

[[{'False': False, 'None': None, 'True': True}, {u'paginator': None, u'post_list': [], u'object_list': [ ],...,]]

我的测试出了什么问题?我怎样才能做这个测试?泰

最佳答案

class BlogListViewTests(TestCase):

def setUp(self):
self.resp = self.client.get('/blog/')

def test_has_a_post_list(self):
self.assertTrue('post_list' in self.resp.context)

关于python - 如何测试为 CBV ListView 生成的上下文中是否存在 'object_list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22434217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com