Monday, August 6, 2012

Re: Testing FormWizard

On Tuesday, 7 August 2012 10:30:22 UTC+8, jscn wrote:
> On Monday, 9 July 2012 16:43:19 UTC+8, James Rivett-Carnac wrote:
>
> > 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
>
>
>
> 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'}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/MaWDVfBJsZ8J.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate