问题描述
我有一个观点,即应设置用于基于GET值的表单字段的初始值。我想测试一下我目前正在使用,但我可以查看其他工具。
I have a view that should be setting an initial value for a form field based on a GET value. I want to test this. I'm currently using Django's test client but I am open to looking at other tools.
对不起,我没有提到我很清楚 assertContains 方法,但我希望有一个比搜索更好的方法该HTML用于输入
标记和值
属性。
Sorry, I did not mention that I am well aware of the assertContains method but I was hoping there was a better way other than searching the HTML for an input
tag and the value
attribute.
推荐答案
恨回答我自己的问题(像我第三次这样做),但在嘲笑测试客户端之后,我找到了一个更好的方法:
Hate to answer my own question (like the 3rd time I've done it) but after mocking around with the test client, I've found a better way:
def test_creating_stop(self):
c = self.client
# Check that name is pre-filled
response = c.get('%s?name=abcd' % reverse('add_new_stop'))
self.assertEqual(response.context['form'].initial['name'], 'abcd')
有谁看到什么错呢?我会留下一段时间看看人们的想法。
Does anyone see anything wrong with this? I'll leave it up for a while see what people think.
这篇关于Django测试:测试表单域的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!