我正在尝试在具有多个复选框的表单上运行单元测试,但无法弄清楚如何发送 POST 数据。我能找到的最相似的问题是here。但是我如何将该 url 编码的部分与其他表单数据一起嵌入到 POST 中?
如果我这样做,我的测试会出错并说 classes = request.POST.getlist('class_choices') AttributeError: 'dict' object has no attribute 'getlist':
request = HttpRequest()
request.method = 'POST'
request.POST['fname'] = 'A'
request.POST['lname'] = 'Student'
request.POST['email'] = 'me@name.com'
request.POST['class_choices'] = urllib.urlencode({
'class_choices': ['1', '2'],
}, True)
request.POST['passwd'] = 'password'
request.POST['conpasswd'] = 'password'
response = success(request)
self.assertIn('My Browser', response.content.decode())
但是如果我这样做,我会在 fname = request.POST['fname'] TypeError: string indices must be integers, not str 上得到一个错误
request.POST = urllib.urlencode({
'class_choices': ['1', '2'],
'fname': 'A',
'lname': 'Student',
'email': 'me@name.com',
'passwd': 'password',
'conpasswd': 'password'
}, True)
response = success(request)