22

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.

2
  • Try using the rename feature built into Visual Studio. That should help resolve all those references for you.
    – casademora
    Commented Sep 30, 2008 at 4:59
  • Rename feature? Never use it; I use VA similar feature and it misses out on all the view reference! Ouch
    – Graviton
    Commented Sep 30, 2008 at 6:55

5 Answers 5

8

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!

6

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>
3

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.

1

T4MVC to avoid magic values

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 a view? Testing static non functional things?

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.

1

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...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.