Fundamentals of Unit Testing: Unit Testing of MVC Application

Fundamentals of unit testing: Unit testing of MVC application

This is the “Fundamentals of unit testing” article series. Here we are discussing various concepts of unit testing, In our previous article we discussed various important concepts, you can read them here.

In this article we will learn to perform unit testing in MVC applications. We know that, one of the fundamental ideas of the MVC architecture is to make unit testing of applications easy. So, let's create a MVC application and configure a small controller in that.
In this example, we have implemented studentController and it contains the ReturnStudent() action. The action will return a specific view depending on input parameters. If we pass “1” as the parameter then it will return “bsc” view and if the parameter value is “2” then it will return “mca” view. And if the parameter is other then 1 or 2 then it will return some text. Have a look at the following code:


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  7. namespace MVC.Controllers  
  8. {  
  9.     public class studentController : Controller  
  10.     {  
  11.         public ActionResult ReturnStudent(int type)  
  12.         {  
  13.             if (type == 1)  
  14.                 return View("bsc");  
  15.             else if (type == 2)  
  16.                 return View("mca");  
  17.             else  
  18.                 return Content("View does not match with parameter");  
  19.         }  
  20.     }  
  21. }  
Fine, now we will implement our unit test class. So, add one unit test application in the same solution. Please don't forget to add a reference for the “System.Web.Mvc” library, since we will use some class that belongs to this library. Here is sample code implementation.


  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  4. using MVC.Controllers;  
  5. using TestProjectLibrary;  
  6. using System.Web.Mvc;  
  8. namespace UnitTest  
  9. {  
  10.     [TestClass]  
  11.     public class UnitTest1  
  12.     {  
  13.         [TestMethod]  
  14.         public void TestMVC()  
  15.         {  
  16.             studentController obj = new studentController();  
  17.             var result = obj.ReturnStudent(1) as ViewResult;  
  18.             Assert.AreEqual("bsc", result.ViewName);  
  19.         }  
  20.     }  
  21. }  
Here we are invoking the ReturnStudent() function by passing the argument "1"; that means it will throw the “bsc” view. The result we are converting to a ViewResult object and storing it to the “result” variable. Then we are checking the ViewName property to detect whether or not it has thrown the correct view. Here is sample output of the test.

Testing Output

The test got passed because we sent “1” and the view name is “bsc”.

Check TempData in unit test

Not only view, we can also check TempData in unit testing. In this example we are setting some data into TempData and then we are returning a view. So, when the view is returned, along with the view, TempData will also be returned.

  1. public class studentController : Controller  
  2. {  
  3.     public ActionResult ReturnStudent(int type)  
  4.     {  
  5.         if (type == 1)  
  6.         {  
  7.             TempData["name"] = "sourav";  
  8.             return View("bsc");  
  9.         }  
  10.         else if (type == 2)  
  11.         {  
  12.             return View("mca");  
  13.         }  
  14.         else  
  15.             return Content("View does not match with parameter");         
  16.     }  
  17. }  
Here is the code for unit testing. In this case we are checking the TempData[] property of the returned view.
  1. [TestMethod]  
  2. public void TestMVC()  
  3. {  
  4.     studentController obj = new studentController();  
  5.     var result = obj.ReturnStudent(1) as ViewResult;  
  6.     Assert.AreEqual("sourav", result.TempData["name"]);  
  7. }  
The test has passed because we set “sourav” in TempData and we are checking it within the AreEqual() function of the Assert class.

Checking View in Unit Testing

Check ViewData in unit test

In the same way we can check that ViewData is associated with the view. Here is a small example. At first we are setting ViewData within the controller and then returning a view.

  1. public ActionResult ReturnStudent(int type)  
  2. {  
  3.     if (type == 1)  
  4.     {  
  5.         ViewData["name"] = "souravkayal";  
  6.         return View("bsc");  
  7.     }  
  8.     else if (type == 2)  
  9.     {  
  10.         return View("mca");  
  11.     }  
  12.     else  
  13.         return Content("View does not match with parameter");  
  14. }  
Here is the unit test class to check whether or not the result is the correct ViewData.
  1. [TestMethod]  
  2. public void TestMVC()  
  3. {  
  4.     studentController obj = new studentController();  
  5.     var result = obj.ReturnStudent(1) as ViewResult;  
  6.     Assert.AreEqual("sourav", result.ViewData["name"]);  
  7. }  

We have seen how a unit test is easy to perform in a MVC application. In future few articles we will understand a few more concepts of unit testing. Happy learning.

Similar Articles