gpt4 book ai didi

python - 如何在使用 django.test 测试用例时从 View 获取对象

转载 作者:行者123 更新时间:2023-12-01 03:45:59 25 4
gpt4 key购买 nike

我正在尝试访问将发送到模板的对象“ Wine ”。基本上,在这个例子中,我的 Wine 是以瓶装、玻璃杯或两者兼而有之的方式出售的。我正在进行的第一个测试应该能够检索发送到模板的名为“wines”的对象中的所有 3 种 Wine 。 (create_wine() 是一个自定义 Wine.objects.create() 方法)。

如果您注意到,我正在使用 django.test import TestCase,因此我有一个 self.client 对象可供使用。另外,如果您注意到的话,我正在向您展示我试图在“(调试)”文本中进行调试的位置。

我真正想要得到的是预渲染的 json。在我看来, View 使用对象来渲染 html,以创建它需要的 html 并返回它。那么我如何访问这个预渲染的对象呢?

问题是我将使用相同的 View 来渲染这些 Wine 对象。如果可能的话,我想使用相同的模板,这意味着将数据发送到 View 并重写它,以便它在渲染之前获取正确的 Wine 。我认为这还可以。如果这违反了 django 方法论,我洗耳恭听。

还有其他方法可以解决这个问题吗?还是我已经很接近了?

代码

View

def wine_list(request):
wines = Wine.objects.filter(is_active=True)
return render(request, 'wine/wine_list.html', {'wines': wines})

网址

urlpatterns = [
url(r'^$', 'wine.views.wine_list', name='wine_list'),
url(r'^bottle/$', 'wine.views.wine_list', name='wine_list_bottle'),
url(r'^glass/$', 'wine.views.wine_list', name='wine_list_glass'),
url(r'^([0-9]+)/$', 'wine.views.wine_details', name='wine_detail'),
]

UT

    from django.test import TestCase

def test_both_wine_glass_and_bottle_pull_different_objects(self):

# Todo: how to pull object info from view
self.create_wine(container="bottle")
self.create_wine(container="glass")
self.create_wine(container="both")

request = self.client.get("/wine/")
from wine.views import wine_list
result = wine_list(request)
(debug) result

# assert wine/ wine is both glass and bottle
# assert wine/glass/ wine is only both or glass wines
# assert wine/bottle/ wine is only both or bottle wines
self.fail("finish the test")

“结果”(调试)

result = {HttpResponse} <HttpResponse status_code=200, "text/html; charset=utf-8">
_charset = {NoneType} None
_closable_objects = {list} <class 'list'>: []
_container = {list} <class 'list'>: [b'<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Wine List</title>\n \n <link rel="stylesheet" href="/static/wine/reset.css">\n <link rel="stylesheet" href="/static/wine/menu.css">\n</head>\n<bod
_handler_class = {NoneType} None
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')}
_reason_phrase = {NoneType} None
charset = {str} 'utf-8'
closed = {bool} False
content = {bytes} b'<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <title>Wine List</title>\n \n <link rel="stylesheet" href="/static/wine/reset.css">\n <link rel="stylesheet" href="/static/wine/menu.css">\n</head>\n<body>\n <header c
cookies = {SimpleCookie}
reason_phrase = {str} 'OK'
status_code = {int} 200
streaming = {bool} False

其他通过的单元测试(如果有帮助的话)

    self.create_wine(container="both")

bottle = Wine.bottle.all()
glass = Wine.glass.all()
both = Wine.objects.all()

self.assertEqual(2, len(bottle))
self.assertEqual(2, len(glass))
self.assertEqual(3, len(both))

def test_both_wine_glass_and_bottle_pull_the_same_template(self):
bottle_list = self.client.get('/wine/bottle/')
glass_list = self.client.get('/wine/glass/')
both_list = self.client.get('/wine/')

self.assertTemplateUsed(both_list, 'wine/wine_list.html')
self.assertTemplateUsed(bottle_list, 'wine/wine_list.html')
self.assertTemplateUsed(glass_list, 'wine/wine_list.html')

简单模板 wine_list.html

{% extends 'wine/base.html' %}
{% block content %}
<section id="wine_content">
<div class="cards">

{% for wine in wines %}
<div class="card">
<a href="/wine/{{ wine.id }}">
<h4>{{ wine.name }}</h4>
<p>{{ wine.vintage }}</p>
<p>{{ wine.description}}</p>
</a>
</div>
{% endfor %}

</div>
</section>

{% endblock %}

最佳答案

您误解了测试客户端的工作原理。当您调用 self.client.get("/wine/") 时,它会模拟对 /wine/ 的请求,并调用您的 wine_list看法。您不必手动调用 wine_list

client.get() 调用返回响应。然后,您可以使用响应进行测试断言,并从 response.context 获取项目。 .

    response = self.client.get("/wine/")
self.assertEqual(response.status_code, 200) # check 200 OK response
wines = response.context['wines'] # this is the list of wines you included in the context
# check that wines is as you expected.
for wine in wines:
# All wines should be active
self.assertTrue(wine.is_active)
...

关于python - 如何在使用 django.test 测试用例时从 View 获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38921202/

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