Any idea on how to unit test the views in ASP.NET MVC?
I am sick of encountering the yellow screen of death when I launch my MVC project just because I forget to update the views when one of the Action
methods of my controller changes name.
Any idea on how to unit test the views in ASP.NET MVC?
I am sick of encountering the yellow screen of death when I launch my MVC project just because I forget to update the views when one of the Action
methods of my controller changes name.
You could write integration tests using Watin, but if you just need a quick check to see if you've any errors in your views, you could also try the solution mentioned in this post: How can I compile ASP.NET pages before loading them with a webserver. Prebuild your aspx pages and you're good to go!
Set <MvcBuildViews>
to true in your project file and the compiler will let you know about this sort of problem whenever you build.
This assumes that your project file also contains the following section (gets added automatically in ASP.NET MVC 1.0)
<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>
Well, in addition to the Stephen Walther's blog entry noted by AugustLights, there are some other options as well...
Jim Zimmerman talks about on his blog about some code he wrote to dynamically precompile his ASP.NET MVC view pages to find any simple mistakes.
You could also use the Spark View Engine, which has the feature of precompilation, instead of using the default ASPX View Engine that is enabled by ASP.NET MVC. This is a pretty dramatic change and might not be an option for you ;)
If you want more information about Spark or other View Engines, Scott Hanselman has a great blog post describing them and what they might look like.
Your problem boils down to magic strings doesn't it. So why not try solving those?
T4MVC is about exactly this to avoid those nasty magic strings. So whenever you'd rename an action method, your compilation would break because old action references wouldn't exist any more.
Unit testing is about testing functional aspects of your application and not static ones (in your case that would be links in someone else's case it would be some I don't know... CSS classes or particular HTML elements on the page). That just doesn't buy me to do unit testing on views...
So instead unit testing your views solve your problem at its root.
S. Walther has something that addresses this, but it looks a little cumbersome... http://stephenwalther.com/archive/2008/07/26/asp-net-mvc-tip-25-unit-test-your-views-without-a-web-server
I'm gonna look into this some more...seems like a reasonable thing to do...