gpt4 book ai didi

django - Django在测试中测试存储的 session 数据

转载 作者:行者123 更新时间:2023-12-03 15:14:25 26 4
gpt4 key购买 nike

我有这样的看法:

def ProjectInfo(request):
if request.method == 'POST':
form = ProjectInfoForm(request.POST)
if form.is_valid():
# if form is valid, iterate cleaned form data
# and save data to session
for k, v in form.cleaned_data.iteritems():
request.session[k] = v
return HttpResponseRedirect('/next/')
else:
...
else:
...


在我的测试中:

from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from tool.models import Module, Model
from django.contrib.sessions.models import Session

def test_project_info_form_post_submission(self):
"""
Test if project info form can be submitted via post.
"""
# set up our POST data
post_data = {
'zipcode': '90210',
'module': self.module1.name,
'model': self.model1.name,
'orientation': 1,
'tilt': 1,
'rails_direction': 1,
}
...
self.assertEqual(response.status_code, 302)
# test if 'zipcode' in session is equal to posted value.


因此,在我的测试中,最后一条注释在哪里,我想测试 session字典中是否有特定值,以及key:value配对是否正确。我该怎么办?我可以使用 request.session吗?

任何帮助,不胜感激。

最佳答案

根据docs

from django.test import TestCase

class SimpleTest(TestCase):
def test_details(self):
# Issue a GET request.
self.client.get('/customer/details/')
session = self.client.session
self.assertEqual(session["somekey"], "test")

关于django - Django在测试中测试存储的 session 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13128443/

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