简体   繁体   中英

How to test Django Form ChoiceField

I've been stuck trying to test a Form ChoiceField in Django.

I have a Form with a single 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)

This is the tuple list of my plan_choices :

[('Bronze', 'Bronze ($10.00 per month)'),
 ('Silver', 'Silver ($20.00 per month)')]

I am trying to test it in the following way:

response = self.client.post(reverse('payment:register_step3'),
    {'plan': 'Bronze'}, follow=True)
self.assertRedirects(response, reverse('payment:register_step4'))

However, when running my tests, I keep getting the error traceback:

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)

I am using:

Django 1.6.8
Python 3.4

I'm thinking this is something easy that I am missing?

thank you

Edit: add View

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

Notes on View:

It just accepts the Form Choice.

It works when I test it manually.

I am just having trouble with the Django ChoiceField Test part because I haven't tested a ChoiceField before.

Thank you

I could bring the test to fail in exactly the same way, if I post an invalid choice. Posting Bronze passes the test, but I have the choices hardcoded in the form class and don't fetch them dynamically from the database.

Makes me think if the plans exist in the database of the unit test (or in a mocked queryset ) so that they are fetched correctly from Plan.objects.get_choices() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM