views:

189

answers:

3

Most of the stackoverflow posts about ASP.NET MVC unit testing talk about a specific issue or specific thing to test such as testing HtmlHelpers. Others on the subject of best practices have surprisingly few answers. Available videos I have watched are completely useless in my opinion - and mind blowingly long (by the time you've watched 1.5 hours and not learned anything).

What I want to know from people who've actually been doing this is :

  • What are the most important things to test first
  • What doesn't need testing (shock horror for me saying that but I'm not after 100%)
  • What is hard to test and how have you overcome difficult thins to test.
  • What things can break in refactoring that a test won't catch.

I'm not new to how to unit test - but I'm very new to actually doing it consistently. I'd really appreciate lessons learned from those who are experts in unit testing ASP.NET MVC.

I'm really looking for specific things you might only find out after having tried it - not jsut general advice like 'use interfaces' - although of course any suggestions are welcome.

Oh and lets say I've decided to use Microsoft's unit testing - just becasue its already there. I think all answers would apply to all testing frameworks though.

+1  A: 

I dont think the answer has to be specific to ASP.NET MVC. Like any other application, the most important thing you have to test is your core logic. That is, your model code and your controller actions.

Eran Kampf
i asked it like that for a reason. for instance there are things related to routing, specific actionresult classes that people who've been using it to test for a while may be able to offer advice on
Simon_Weaver
+6  A: 
  • Test your routing. You should use RouteLink to remove ambiguity when you generate a URL in your View, but when you submit a URL, you are dependent upon the routing system to select the correct route. So test that the URL patterns you support do in fact return the correct route.
  • Test your controller actions. Use a mock repository, and test that manually invoking each action has the results you expect.
  • Test all business logic in your model. This is obvious, and little different from non-MVC applications.
  • Test any custom view helpers you write. Although I don't generally unit test views, view helpers are different.
  • Test your JavaScript. There are unit testing frameworks for this, that testing JavaScript is so easy that such frameworks are hardly necessary. But testing JavaScript is incredibly important, due to the tendency of the language to hide errors from you.
  • If you have written any custom model binders, they need special attention. For one thing, it is a lot easier to debug a model binder via a unit test than when it is "live" in the application.
Craig Stuntz
+2  A: 

I'd like to elaborate on testing controller actions:

  • Verify you get the proper ActionResult. A redirect is different from a view.
  • Also verify the expected view name. If you rely on the default view, it should be empty.
  • Verify you get the proper view model.
  • Verify all branches in your action. Keep them to a minimum and move them to a helper / service when they grow too many.

In short, verify anything from the ActionResult that you will use.

Thomas Eyde