views:

478

answers:

4

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.

+1  A: 

S. Walther has something that addresses this, but it looks a little cumbersome... http://weblogs.asp.net/stephenwalther/archive/2008/07/26/asp-net-mvc-tip-25-unit-test-your-views-without-a-web-server.aspx

I'm gonna look into this some more...seems like a reasonable thing to do...

Codewerks
http://stephenwalther.com/blog/archive/2008/07/26/asp-net-mvc-tip-25-unit-test-your-views-without-a-web-server.aspx is the current link here.
No Refunds No Returns
+5  A: 

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!

Casper
A: 

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.

Elijah Manor
+1  A: 

Set <MvcBuildViews> to true in your project file and the compiler will let you know about this sort of problem whenever you build.

This assumed 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>
Richard Ev