I just found a bug where an HTML form element had a name like “fantastic” and the action had an argument like “f*tasktic.” Since it was a checkbox, we were handling it being unchecked by coalescing to false (i.e., HTML forms submit no value for checkboxes that are unchecked, so we assumed null == unchecked.) Since the name was wrong, we always thought the element was unchecked.
This makes me realize that there are likely other places where we have problems like this.
Aside from writing exhaustive browser driven tests (such as with Selenium) are there any other good tricks for catching this type of error?
I am considering writing a test that looks in all of our spark views and asserts that for each argument in the action a form submits to, there is an HTML form element with the same name as the argument and vice versa. We have several “entire app” tests like this already, so it wouldn’t be totally out of place, but it feels kind of brute force and I was wondering if anyone has a better way to catch this kind of thing.
I have a rule to always use ViewModels and EditorFor templates for to generate any form on the site. This stops this problem dead in its tracks.
Brad Wilson has a great blog series on customizing the default template: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
Just got put on a project where this isn’t the case and I’m taken aback by the barbaric way in which we hand write html forms. Icky!