Testing FormWizard

604 views
Skip to first unread message

James Rivett-Carnac

unread,
Jul 9, 2012, 4:43:19 AM7/9/12
to django...@googlegroups.com
I am trying to write test cases for a django 1.4 CookieWizardView (django.contrib.formtools.wizard.views.CookieWizardView), and I'm not sure how to handle sending multiple posts to the view.

class TestWizardView(TestCase):
    def setUp(self):
        self.form_one_post = QueryDict('field_1=value')
        self.form_two_post = QueryDict('field_2=value2')
        self.c = Client()

    def test_form(self):
        response = self.c.post('/wzd/url/',self.form_one_post)
        self.assertContains(...)
        response = self.c.post('/wzd/url/',self.form_two_post)
        self.assertRedirects(...)  # Successful post should redirect to Done page.

But when I do this, i get:
ValidationError: 'ManagementForm data is missing or has been tampered.'

This makes sense, since I haven't put any post information for the management_form (django.contrib.formtools.wizard.forms.ManagementForm), but I don't know where to get that information/where to put that information or what that information is supposed to be.

Any help on how to resolve this or better ways to implement a view test for a wizard would be appreciated.

James

jscn

unread,
Aug 6, 2012, 10:40:06 PM8/6/12
to django...@googlegroups.com
On Tuesday, 7 August 2012 10:30:22 UTC+8, jscn wrote:
> I've been having the exact same problem. After looking at the unit tests for the form wizard and much messing around, I eventually figured it out. You're right about needing to pass in some extra data in.
>
>
>
> Specifically, you need to add a field to the POST data called 'your_views_name-current_step' with the zero-based value of the step to which you're POSTing. Additionally, you need to prepend the zero-based step number to the names of the other fields you're POSTing.
>
>
>
> Assuming you have some classes like:
>
>
>
> class MyForm(forms.Form):
>
> some_field = forms.CharField()
>
>
>
> class MyWizard(SessionWizardView):
>
> # ...do some stuff...
>
>
>
> And a url like:
>
>
>
> url(r'^wizard/$', views.MyWizard.as_view([MyForm, MyOtherForm]))
>
>
>
>
>
> Then your test needs to look like:
>
> class TestWizardView(TestCase):
>
> def test_form(self):
>
> data = {'my_wizard_view-current_step': '0', '0-some_field': 'whatever'}
>
> response = self.client.post('/wizard/', data=data)
>
> # your assertions here
>
>
>
> Josh

Oops, that should be: data = {'my_wizard-current_step': '0', '0-some_field': 'whatever'}
Reply all
Reply to author
Forward
0 new messages