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:
 

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");

        }

    }

}

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.

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);

        }

    }

}

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.

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");       

    }

}

Here is the code for unit testing. In this case we are checking the TempData[] property of the returned view.

[TestMethod]

public void TestMVC()

{

    studentController obj = new studentController();

    var result = obj.ReturnStudent(1) as ViewResult;

    Assert.AreEqual("sourav", result.TempData["name"]);

}

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.

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");   

}

Here is the unit test class to check whether or not the result is the correct ViewData.

[TestMethod]

public void TestMVC()

{

    studentController obj = new studentController();

    var result = obj.ReturnStudent(1) as ViewResult;

    Assert.AreEqual("sourav", result.ViewData["name"]);

}

Conclusion

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.

Up Next
    Ebook Download
    View all
    Learn
    View all