Saturday, May 19, 2012

Testing Views in Asp.Mvc

For many years, the views (the html returned from an rendered MVC controller) were an extremely difficult thing to test. Like many unreachable things, they were declared “not worth it” and ignored.

Until now,
with ApprovalTests we have created a “simple” way to test the rendered output of a view.  I say “simple” because there is a fair amount of overhead for the 1st test, which means we wanted to give you some resources to get it working for yourself.

I created a short video tutorial, and Jim Counts put together a fabulous 3 part blog + Sample Code.

Now you can decide for yourself how important it is to test the views.

Video :

Blog :

Part 1 – Getting Started
Part 2 – Working with Data
Part 3 – Tackling the Build Server


Saturday, May 5, 2012

The Testing Circle

This is the pattern I find myself in day after day while practicing TDD or BDD.

Step 1?

Usually I start at the whiteboard. It is the easiest place to start as I sketch out the scenario I want to program, but not always. Sometimes a mockup file (image, html, or xml) is provided and I start at the result, sometimes I’m give a scenario already in English, and sometimes I have to fiddle with the code first to see what’s happening.

No Beginning, No Ending

The great thing about the circle is no matter where a start, the flow should always be there in the end. If it isn’t, I take this as a smell that I am neglecting something. Also, while I wrote the arrows going clockwise (the normal way I view this process) they can equally go in reverse.

Smell 1: No Result –> Whiteboard

If the result does trigger the original image on the whiteboard (usually long erased by the time I revisit the code), then this will be a maintenance problem when I try to remember why the code looks the way it does when it changes.

Smell 2: No Code –> English

If the resulting code doesn’t make the comments so easy that they should be deleted for not adding any value, then my intention (so clear when I’m writing the original code) will not be there when my fellow programmers, or myself even a few days later, what the understand the code later.