如何测试 Django Form ChoiceField
How to test Django Form ChoiceField
我一直在尝试在 Django 中测试表单 ChoiceField
。
我有一个 Form
和一个 ChoiceField
:
class PickPlanForm(forms.Form):
"Set the `plan` session cookie for choice here."
plan_choices = Plan.objects.get_choices()
# Field
plan = forms.ChoiceField(required=True, choices=plan_choices)
这是我的 plan_choices
:
的元组列表
[('Bronze', 'Bronze (.00 per month)'),
('Silver', 'Silver (.00 per month)')]
我正在尝试通过以下方式对其进行测试:
response = self.client.post(reverse('payment:register_step3'),
{'plan': 'Bronze'}, follow=True)
self.assertRedirects(response, reverse('payment:register_step4'))
但是,在运行我的测试时,我不断收到错误回溯:
Traceback (most recent call last):
File "/Users/aaron/Documents/djcode/textress_concierge/textress/main/tests/test_views.py", line 170, in test_register_step3
self.assertRedirects(response, reverse('payment:register_step4'))
File "/Users/aaron/Documents/virtualenvs/textress/lib/python3.4/site-packages/django/test/testcases.py", line 263, in assertRedirects
(response.status_code, status_code))
AssertionError: False is not True : Response didn't redirect as expected: Response code was 200 (expected 302)
我正在使用:
Django 1.6.8
Python 3.4
我在想这是我想念的简单事情吗?
谢谢
编辑:添加视图
from django.views.generic import FormView
from braces.views import LoginRequiredMixin
class PickPlanView(LoginRequiredMixin, FormView):
"""
Step #3 of Registration
Pick a Plan, and save the Plan as a `session cookie` before creating
the Stipe Customer/Subscription using the Plan Choice.
"""
template_name = 'main/hotel_form.html'
form_class = PickPlanForm
success_url = reverse_lazy('payment:register_step4')
authenticated_redirect_url = settings.VERIFY_LOGOUT_URL
def get_form_kwargs(self):
"""Add the `request` Obj to Form, so I can set the PlanPick as a
session cookie for the time being."""
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
查看注意事项:
它只接受表单选择。
我手动测试时有效。
我只是在 Django ChoiceField Test
部分遇到了问题,因为我之前没有测试过 ChoiceField
。
谢谢
如果我 post 一个无效的选择,我可以用完全相同的方式使测试失败。发布 Bronze 通过了测试,但我在 form
class 中硬编码了选择并且不从数据库中动态获取它们。
让我思考如果计划存在于单元测试的数据库中(或在模拟的 queryset
中),以便从 Plan.objects.get_choices()
?
中正确获取它们
我一直在尝试在 Django 中测试表单 ChoiceField
。
我有一个 Form
和一个 ChoiceField
:
class PickPlanForm(forms.Form):
"Set the `plan` session cookie for choice here."
plan_choices = Plan.objects.get_choices()
# Field
plan = forms.ChoiceField(required=True, choices=plan_choices)
这是我的 plan_choices
:
[('Bronze', 'Bronze (.00 per month)'),
('Silver', 'Silver (.00 per month)')]
我正在尝试通过以下方式对其进行测试:
response = self.client.post(reverse('payment:register_step3'),
{'plan': 'Bronze'}, follow=True)
self.assertRedirects(response, reverse('payment:register_step4'))
但是,在运行我的测试时,我不断收到错误回溯:
Traceback (most recent call last):
File "/Users/aaron/Documents/djcode/textress_concierge/textress/main/tests/test_views.py", line 170, in test_register_step3
self.assertRedirects(response, reverse('payment:register_step4'))
File "/Users/aaron/Documents/virtualenvs/textress/lib/python3.4/site-packages/django/test/testcases.py", line 263, in assertRedirects
(response.status_code, status_code))
AssertionError: False is not True : Response didn't redirect as expected: Response code was 200 (expected 302)
我正在使用:
Django 1.6.8
Python 3.4
我在想这是我想念的简单事情吗?
谢谢
编辑:添加视图
from django.views.generic import FormView
from braces.views import LoginRequiredMixin
class PickPlanView(LoginRequiredMixin, FormView):
"""
Step #3 of Registration
Pick a Plan, and save the Plan as a `session cookie` before creating
the Stipe Customer/Subscription using the Plan Choice.
"""
template_name = 'main/hotel_form.html'
form_class = PickPlanForm
success_url = reverse_lazy('payment:register_step4')
authenticated_redirect_url = settings.VERIFY_LOGOUT_URL
def get_form_kwargs(self):
"""Add the `request` Obj to Form, so I can set the PlanPick as a
session cookie for the time being."""
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
查看注意事项:
它只接受表单选择。
我手动测试时有效。
我只是在 Django ChoiceField Test
部分遇到了问题,因为我之前没有测试过 ChoiceField
。
谢谢
如果我 post 一个无效的选择,我可以用完全相同的方式使测试失败。发布 Bronze 通过了测试,但我在 form
class 中硬编码了选择并且不从数据库中动态获取它们。
让我思考如果计划存在于单元测试的数据库中(或在模拟的 queryset
中),以便从 Plan.objects.get_choices()
?