Using #Ivonna and #CThru for testing Asp.Net MVC Views, part 2 

Saturday, June 26, 2010 10:05:00 AM

Well I finally got back to continue my ideas on using Ivonna to test MVC sites. This time I need to make sure that the authenticated user should see the list of her.. whatever. I won't bother you with much code this time (it's not that different from the previous part), instead, I'd like to return to the fundamental question of "How do I test this".

My first idea was, I've got something like a WebForms UserControl here, so I'd go for a RenderAction call. So, in my test I'll make sure that this method is called with proper parameters. If you're smarter than that, you can see that I'm tying my test to the implementation. Instead of "what", I'm testing "how". Should I change my implementation to calling RenderPartial or DisplayFor, my test breaks, although I still have my implementation done correctly.

To get it "right" (and you can guess there's no absolutely right way here), I think about what it means, "display a list of thingies". My guess is that it means two things, data and view, put together and displayed as part of the page. Now, getting the correct data is not the concern of this test (you should write a unit test here), but getting the correct partial View is the job for Ivonna. And after a quick Reflector session (or, better, using the built-in TraceAspect from CThru), we realize that all various implementations ultimately go through IViewEngine.FindPartialView. And all we have to do is spy on these calls, collect the arguments, and make sure that there's been a call to this method with a correct argument (that is, the path to your partial). This way, unless you move or rename the partial, the test stays green even if your implementation changes.

This CThru aspect can be useful in many other scenarios, so I'm going to include it in the next version of Ivonna, which will contain lots of other MVC related features.

Copyright 2008 by me
8 + 4 =  

Site Map | Printable View | © 2008 - 2010 Your Company Name

Powered by mojoPortal | HTML 5 | CSS | Design by styleshout