You are currently viewing How to implement Unit Testing in ASP.NET MVC?
Unit Testing in ASP.NET MVC

How to implement Unit Testing in ASP.NET MVC?

How to implement Unit Testing in ASP.NET MVC?

Unit Testing in ASP.NET MVC is a Software Testing Method by which individual units of source code are tested to make sure that they are working and fit for use. in ASP.NET MVC we follow the Controller, Model, and View structure. So here I create one new controller and its action method and implement Unit Testing for that.

Here is the sequence of steps that you can follow while implementing a Unit Testing Code.

Step 1: Create a new ASP.NET MVC Project with MVC Template. Here don’t forgot to check Add unit tests checkbox to create a new Unit Testing project along with the original project.

Create ASP.NET MVC Project
Create ASP.NET MVC Project

Note: You can change the Test Project Name if you want to change.

Once you create a project you can find two projects in solution explorer. One for ASP.NET MVC and another for Unit Testing ( in my case name is MVCUnitTestingDemo.Tests).

View Solution Explorer
View Solution Explorer

Step 2: Create a new controller with some action methods. In our case, I create Employee Controller with the Index() and Employees() action method.

Create a Model for Employee in the Model directory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCUnitTestingDemo.Models
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime JoiningDate { get; set; }
        public int Age { get; set; }
    }
}

Now create EmployeeController class.

using MVCUnitTestingDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCUnitTestingDemo.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Employees()
        {
            var employees = from e in GetEmployeeList()
                            orderby e.ID
                            select e;
            return View(employees);
        }
        [NonAction]
        public List<Employee> GetEmployeeList()
        {
            return new List<Employee>()
            {
                new Employee()
                {
                    ID=1,
                    Age=21,
                    JoiningDate= DateTime.Now,
                    Name ="A"
                },
                new Employee()
                {
                    ID=2,
                    Age=22,
                    JoiningDate= DateTime.Now,
                    Name ="B"
                },
                new Employee()
                {
                    ID=3,
                    Age=23,
                    JoiningDate= DateTime.Now,
                    Name ="C"
                },
                new Employee()
                {

                    ID=4,
                    Age=24,
                    JoiningDate= DateTime.Now,
                    Name ="D"
                },
            };
        }
    }
}

In the above example, GetEmployeeList() is a Non-action method and used to return a list of an employee.

Once you done with the EmployeeController class, create a list view for Employees() action method. To add View for Employees action method, right-click on Employees action and select Add View…

Add new view in ASP.NET MVC
Add new view in ASP.NET MVC

Step 3: Test the Methods from EmployeeController into Unit Testing Project. You may notice that the HomeControllerTest.cs controller is already there with some Test class and Test Methods.

All the code perform within Tests Project (Here MVCUnitTestingDemo.Tests)

We need to create our new Test Controller. So right-click on Controller Folder -> Add -> Unit Test…

Remane the UnitTest1.cs with EmployeeControllerTest.cs (for this example) and Implement the following logic inside that.

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MVCUnitTestingDemo.Controllers;
using MVCUnitTestingDemo.Models;

namespace MVCUnitTestingDemo.Tests.Controllers
{
    [TestClass]
    public class EmployeeControllerTest
    {
        [TestMethod]
        public void Index()
        {
            //Arrange
            EmployeeController employeeController = new EmployeeController();
            //Act
            ViewResult result = employeeController.Index() as ViewResult;
            //Assert
            Assert.IsNotNull(result);
        }
        [TestMethod]
        public void Employees()
        {
            //Arrange
            EmployeeController employeeController = new EmployeeController();
            //Act
            ViewResult result = employeeController.Employees() as ViewResult;
            //Assert
            Assert.IsNotNull(result);
        }
        [TestMethod]
        public void NoOfEmployeesIs4()
        {
            //Arrange
            EmployeeController employeeController = new EmployeeController();
            //Act
           List<Employee> employeelist= employeeController.GetEmployeeList() as List<Employee>;
            //Assert
            Assert.IsTrue(employeelist.Count == 4);
        }

    }
}

Let’s talk about the Method within the above test controller.

  1. Index() – check Index() method of EmployeeController is working properly or not.
  2. Employees() – check Employees() method of EmployeeController is working properly or not.
  3. NoOfEmployeesIs4() – fetch employee list from  GetEmployeeList() method and check whether the employee count is equal to 4 or not.

Once you are done with all that, you need to run all or particular Test case to check whether it is a success or not. To test these three action methods, go to the Test menu. Select Run → All Tests to test these methods. You can check the test result in Test Explorer.

Run all Tests
Run all Tests

Test Explorer

[Download Source Code]

Here is the GitHub link from where you can download the entire project. Link: https://github.com/Jignesh-Darji/Unit-Testing-in-ASP.NET-MVC