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.
- Fundamentals of Unit Testing: Getting Started With Unit Testing
- Fundamentals of Unit Testing: Test Your Application by Visual Studio Unit Test
- Fundamentals of Unit Testing: Understand AAA in Unit Testing
- Fundamental of Unit Testing: Understand AreEqual and AreEqual<T> in Unit Testing
- Fundamental of Unit Testing: Test Initialize and Test Setup
- Fundamentals of Unit Testing: Understand CollectionAssert() in Unit Testing
- Fundamentals of Unit Testing: Understand ExpectedException in Unit Testing
- Fundamentals of Unit Testing: Don't Test Your Private Method
- Fundamentals of Unit Testing: Unit Test Using Nunit
- Fundamentals of Unit Testing: Understand Mock Object in Unit Testing
- Fundamentals of Unit Testing: Unit Testing of IOC Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MVC.Controllers
- {
- public class studentController : Controller
- {
- public ActionResult ReturnStudent(int type)
- {
- if (type == 1)
- return View("bsc");
- else if (type == 2)
- return View("mca");
- else
- return Content("View does not match with parameter");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using MVC.Controllers;
- using TestProjectLibrary;
- using System.Web.Mvc;
- namespace UnitTest
- {
- [TestClass]
- public class UnitTest1
- {
- [TestMethod]
- public void TestMVC()
- {
- studentController obj = new studentController();
- var result = obj.ReturnStudent(1) as ViewResult;
- Assert.AreEqual("bsc", result.ViewName);
- }
- }
- }

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.
- public class studentController : Controller
- {
- public ActionResult ReturnStudent(int type)
- {
- if (type == 1)
- {
- TempData["name"] = "sourav";
- return View("bsc");
- }
- else if (type == 2)
- {
- return View("mca");
- }
- else
- return Content("View does not match with parameter");
- }
- }
- [TestMethod]
- public void TestMVC()
- {
- studentController obj = new studentController();
- var result = obj.ReturnStudent(1) as ViewResult;
- Assert.AreEqual("sourav", result.TempData["name"]);
- }

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.
- public ActionResult ReturnStudent(int type)
- {
- if (type == 1)
- {
- ViewData["name"] = "souravkayal";
- return View("bsc");
- }
- else if (type == 2)
- {
- return View("mca");
- }
- else
- return Content("View does not match with parameter");
- }
- [TestMethod]
- public void TestMVC()
- {
- studentController obj = new studentController();
- var result = obj.ReturnStudent(1) as ViewResult;
- Assert.AreEqual("sourav", result.ViewData["name"]);
- }
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.

Hamid KhanPosted Oct 13, 2017, 5:53 AM
Nice Collection ..............................Thanks
Matthew McDonaldPosted May 27, 2017, 4:53 PM
How would you test TempData for a controller method the returns a RedirectToRoute ActionResult?
Gowtham RajamanickamPosted Apr 25, 2015, 5:28 AM
good
Khan EngineerPosted Feb 5, 2015, 7:23 AM
please can you explain how to add unit test in an existing MVC project with references