Create Unit Test For MVC3 Applications
B Sahoo Posted On : 20/02/2012
About Author
Biswaranjan Sahoo have good working and teching experience in Microsoft technology.

Unit Test

All tests have been created with one aim, to improve the quality of program code, to make applications and systems more reliable and stable.

In computer programming, unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use or not.

Unit testing can be time-consuming and tedious. It demands patience and thoroughness on the part of the development team. Rigorous documentation must be maintained. Unit testing must be done with an awareness that it may not be possible to test a unit for every input scenario that will occur when the program is run in a real-world environment.
Something every developer has to do when creating a website/online application is test their work. My passion is achieving zero defects so in my MVC application I want to see how I can work with Unit Testing.

In recent years, Test-Driven Development (TDD) has become a hot technique in software development industry. According to the TDD, we should first write unit tests based on product specifications, and then write the source code that is required to make the tests succeed. On response of this idea, Visual Studio provides a couple of related facilities, especially an excellent built-in unit testing framework.

In this series of articles, I will provide you details about unit testing for ASP.NET MVC 3.0 applications. I cover how to

  • Test simple controller actions
  • Test actions that interact with a database
  • Test actions that use the user’s session

System Requirements

You must have the following items to complete this lab:

  • ASP.NET and ASP.NET MVC 3
  • Visual Studio 2010 Professional or superior
  • SQL Server Database (Express edition or above)

The A-A-A pattern

Before starting unit test for MVC3 application, I want to give some idea regarding A-A-A pattern.

To write meaningful unit tests that can be well-read, developers are suggested to follow the A-A-A (Arrange-Act-Assert) pattern. In detail, first we arrange all necessary preconditions and inputs. Then, we act on the object or method under test. Finally, we assert that we have the desired result. In practice, this can be translated easily into testing ASP.NET MVC controllers:

1. Arrange: Instantiate a controller object (in DI scenarios, you may need to supply mock versions of any dependencies as constructor parameters).

2. Act: Run an action method, passing sample parameters and collecting the ActionResult.

3. Assert: Assert that the ActionResult describes the expected result.

Testing simple controller actions

Task: 1:: Create Controller

  • Start Microsoft Visual Studio 2010 Professional from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  • In the File menu, choose New Project.
  • Choose Asp.net Mvc3 Web Application, Give name “MVC3_UnitTestApplication” to the project, choose Internet Application and check the check box for Create a unit test Project.
  • Right click on Controller for creation of new controller and create Product Controller
  • Add ActionResult “Detail” to Product Controller. In Index ActionResult add Message to ViewBag.
  • The ProductController contains two action methods named Index() and Details(). Both action methods return a view. Notice that the Details() action accepts a parameter named Id. we want to test whether or not the ProductController returns the right view. We want to make sure that when the ProductController.Details() action is invoked, the Details view is returned. The test class in Listing 2 contains a unit test for testing the view returned by the ProductController.Details() action.

Task: 2:: Create Unit Test Controller

  • Right click on controller folder of MVC3_UnitTestApplication.Test Project, select Unit Test.
  • Select Unit Test and give Test Name as “ProductControllerTest.cs”. Then Click OK. It creates a class file with name “ProductControllerTest” in controller folder of Test Project.
  • Remove all default methods of ProductControllerTest Class.Add 3 namespace (System.Web.Mvc,MVC3_UnitTestApplication, MVC3_UnitTestApplication.Controllers) to ProductControllerTest.
  • Add Index Method to ProductControllerTest Class with [TestMethod] attribute.
  • Then go for A-A-A (Arrange-Act-Assert) pattern. Create Object of controller class as Arrange. Create result type as Act & start comparing the actual and expected result as Assert.

Task: 3:: Test Unit Test Application

  • You can run the unit test by either entering the keyboard combination Ctrl-R, A or by clicking the Run All Tests in Solution button.
  • If the test passes, you’ll see the Test Results window in
  • If I did a change in assert, such as I write

    Assert.AreEqual("Today's Date: " + dtNow.ToString(), result.ViewBag.Message);

    In assert, then Test Result shows failed. On right click of result in result window , we can get details of Result.

Post your Suggetion or Comment
Name :
Email Address :
Comment :