Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the most important things to test in an ASP.NET MVC application?

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.

like image 519
Simon_Weaver Avatar asked Feb 09 '09 05:02

Simon_Weaver


People also ask

What are the 3 main components of an ASP.NET MVC application?

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.

What is ASP.NET MVC unit testing?

ASP.NET Core API. In computer programming, unit testing is a software testing method by which individual units of source code are tested to determine whether they are fit for use.

How do I test .NET applications?

To test an application, you need to add a Unit Test project to the ASP.Net solution. All tests can be made to run in Visual Studio. A test explorer will show the results of all of the tests.


2 Answers

  • 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.
like image 168
Craig Stuntz Avatar answered Nov 15 '22 20:11

Craig Stuntz


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.

like image 33
Thomas Eyde Avatar answered Nov 15 '22 20:11

Thomas Eyde