What is Unit Testing?
It is all about testing of every smallest testable block of a code in an automated manner.
Overview of the Repository Pattern
The Repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. It is a data access pattern that prompts a more loosely coupled approach to data access. We create the data access logic in a separate class, or set of classes, called a repository, with the responsibility of persisting the application's business model.
As the Repository Pattern is useful for decoupling entity operations from presentation, it allows easy mocking and unit testing.
Getting Started
Create a new Project. Open Visual Studio 2012.
File - new - Project
Select Visual C#, then Web in installed templates.
Select ASP.NET MVC 4 Web Application.
Enter the Name and choose the location.
(here, I am giving the name as mvcunittest)
Click OK.

Click OK button.
In the next wizard there is a check box for creating a unit test project. For creating a unit test project select (check) that check box. We can add it later also or also add a new item feature.

Now, we can see the SolutionExplorer as given below:

Now adding a new ADO.NET Entity Data Model and providing it a relevant name.



Click Next and Click New Connection.
Here, I am selecting Microsoft SQL Server option.

Click Continue,

Click Test Connection


From the tables list I am selecting emp table as given below:

Click Finish

Now add a new class in Models:
Right click on Models folder - Add - class
Give the name as EmpRepository.cs
Now my code in EmpRepository.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace mvcunittest.Models
- {
- public class EmpRepository : IEmployeeRepository
- {
- private vrEntities _db = new vrEntities();
- public IEnumerable<emp> GetAllEmployee()
- {
- return _db.emps.ToList();
- }
- public void CreateNewEmployee(emp employeeToCreate)
- {
- _db.emps.Add(employeeToCreate);
- _db.SaveChanges();
- }
- public void DeleteEmployee(int id)
- {
- var conToDel = GetEmployeeByID(id);
- _db.emps.Remove(conToDel);
- _db.SaveChanges();
- }
- public emp GetEmployeeByID(int id)
- {
- return _db.emps.FirstOrDefault(d => d.empno == id);
- }
- public int SaveChanges()
- {
- return _db.SaveChanges();
- }
- }
- public interface IEmployeeRepository
- {
- IEnumerable<emp> GetAllEmployee();
- void CreateNewEmployee(emp employeeToCreate);
- void DeleteEmployee(int id);
- emp GetEmployeeByID(int id);
- int SaveChanges();
- }
- }
(give the name as EmployeeController)


Code in EmployeeController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using mvcunittest.Models;
- namespace mvcunittest.Controllers
- {
- public class EmployeeController : Controller
- {
- IEmployeeRepository _repository;
- public EmployeeController() : this(new EmpRepository()) { }
- public EmployeeController(IEmployeeRepository repository)
- {
- _repository = repository;
- }
- ////
- //// GET: /Employee/
- public ViewResult Index()
- {
- ViewData["ControllerName"] = this.ToString();
- return View("Index", _repository.GetAllEmployee());
- }
- ////
- //// GET: /Employee/Details/5
- public ActionResult Details(int id = 0)
- {
- //int idx = id.HasValue ? (int)id : 0;
- emp cnt = _repository.GetEmployeeByID(id);
- return View("Details", cnt);
- }
- //
- // GET: /Employee/Create
- public ActionResult Create()
- {
- return View("Create");
- }
- //
- // POST: /Employee/Create
- [HttpPost]
- public ActionResult Create([Bind(Exclude = "Id")] emp employeeToCreate)
- {
- try
- {
- if (ModelState.IsValid)
- {
- _repository.CreateNewEmployee(employeeToCreate);
- return RedirectToAction("Index");
- }
- }
- catch (Exception ex)
- {
- ModelState.AddModelError("", ex);
- ViewData["CreateError"] = "Unable to create; view innerexception";
- }
- return View("Create");
- }
- //
- // GET: /Employee/Edit/5
- public ActionResult Edit(int id = 0)
- {
- var employeeToEdit = _repository.GetEmployeeByID(id);
- return View(employeeToEdit);
- }
- //
- // GET: /Employee/Edit/5
- [HttpPost]
- public ActionResult Edit(int id, FormCollection collection)
- {
- emp cnt = _repository.GetEmployeeByID(id);
- try
- {
- if (TryUpdateModel(cnt))
- {
- _repository.SaveChanges();
- return RedirectToAction("Index");
- }
- }
- catch (Exception ex)
- {
- if (ex.InnerException != null)
- ViewData["EditError"] = ex.InnerException.ToString();
- else
- ViewData["EditError"] = ex.ToString();
- }
- #if DEBUG
- foreach (var modelState in ModelState.Values)
- {
- foreach (var error in modelState.Errors)
- {
- if (error.Exception != null)
- {
- throw modelState.Errors[0].Exception;
- }
- }
- }
- #endif
- return View(cnt);
- }
- //
- // GET: /Employee/Delete/5
- public ActionResult Delete(int id)
- {
- var conToDel = _repository.GetEmployeeByID(id);
- return View(conToDel);
- }
- //
- // POST: /Employee/Delete/5
- [HttpPost]
- public ActionResult Delete(int id, FormCollection collection)
- {
- try
- {
- _repository.DeleteEmployee(id);
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- }
- }

Now add View by right-clicking on Index action method in the controller and select strongly typed view and select model class which is created by a data model and select List from scaffold template and click Add.


You can add views in the same way for Create, Delete, Details. Edit scaffold templates.
After adding all the appropriate views, now go to Index.cshtml and we can see some code which is commented as given below,

Now, remove the comment make some changes as the following,

Similarly go to Details.cshtml, remove the comments and make the changes as given below,

Now let's run the application to see the result.

We are now OK with the MVC application using a repository and the Entity Framework.
Now it is the time to work on Unit Testing. First of all create a models folder in the test project:

Give the folder name as Models.


Code in InmemoryEmpRepository.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using mvcunittest.Models;
- namespace mvcunittest.Tests.Models
- {
- class InmemoryEmpRepository : IEmployeeRepository
- {
- private List<emp> _db = new List<emp>();
- public Exception ExceptionToThrow { get; set; }
- public IEnumerable<emp> GetAllEmployee()
- {
- return _db.ToList();
- }
- public emp GetEmployeeByID(int id)
- {
- return _db.FirstOrDefault(d => d.empno == id);
- }
- public void CreateNewEmployee(emp employeeToCreate)
- {
- if (ExceptionToThrow != null)
- throw ExceptionToThrow;
- _db.Add(employeeToCreate);
- }
- public void SaveChanges(emp employeeToUpdate)
- {
- foreach (emp employee in _db)
- {
- if (employee.empno == employeeToUpdate.empno)
- {
- _db.Remove(employee);
- _db.Add(employeeToUpdate);
- break;
- }
- }
- }
- public void Add(emp employeeToAdd)
- {
- _db.Add(employeeToAdd);
- }
- public int SaveChanges()
- {
- return 1;
- }
- public void DeleteEmployee(int id)
- {
- _db.Remove(GetEmployeeByID(id));
- }
- }
- }


Code in EmployeeControllerTest.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using mvcunittest.Controllers;
- using mvcunittest.Models;
- using mvcunittest.Tests.Models;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System.Web.Mvc;
- using System.Web.Routing;
- using System.Web;
- using System.Security.Principal;
- namespace mvcunittest.Tests.Controllers
- {
- [TestClass]
- public class EmployeeControllerTest
- {
- /// <summary>
- /// This method used for index view
- /// </summary>
- [TestMethod]
- public void IndexView()
- {
- var empcontroller = GetEmployeeController(new InmemoryEmpRepository());
- ViewResult result = empcontroller.Index();
- Assert.AreEqual("Index", result.ViewName);
- }
- /// <summary>
- /// This method used to get employee controller
- /// </summary>
- /// <param name="repository"></param>
- /// <returns></returns>
- private static EmployeeController GetEmployeeController(IEmployeeRepository emprepository)
- {
- EmployeeController empcontroller = new EmployeeController(emprepository);
- empcontroller.ControllerContext = new ControllerContext()
- {
- Controller = empcontroller,
- RequestContext = new RequestContext(new MockHttpContext(), new RouteData())
- };
- return empcontroller;
- }
- /// <summary>
- /// This method used to get all employye listing
- /// </summary>
- [TestMethod]
- public void GetAllEmployeeFromRepository()
- {
- // Arrange
- emp employee1 = GetEmployeeName(1, "ravi", 50000, 10);
- emp employee2 = GetEmployeeName(2, "suri", 50000, 10);
- InmemoryEmpRepository emprepository = new InmemoryEmpRepository();
- emprepository.Add(employee1);
- emprepository.Add(employee2);
- var controller = GetEmployeeController(emprepository);
- var result = controller.Index();
- var datamodel = (IEnumerable<emp>)result.ViewData.Model;
- CollectionAssert.Contains(datamodel.ToList(), employee1);
- CollectionAssert.Contains(datamodel.ToList(), employee2);
- }
- /// <summary>
- /// This method used to get emp name
- /// </summary>
- /// <param name="id"></param>
- /// <param name="lName"></param>
- /// <param name="fName"></param>
- /// <param name="title"></param>
- /// <param name="address"></param>
- /// <param name="city"></param>
- /// <param name="region"></param>
- /// <param name="postalCode"></param>
- /// <returns></returns>
- emp GetEmployeeName(int id, string lName, int s, int d)
- {
- return new emp
- {
- empno = id,
- ename = lName,
- sal = s,
- deptno = d
- };
- }
- /// <summary>
- /// This test method used to post employee
- /// </summary>
- [TestMethod]
- public void Create_PostEmployeeInRepository()
- {
- InmemoryEmpRepository emprepository = new InmemoryEmpRepository();
- EmployeeController empcontroller = GetEmployeeController(emprepository);
- emp employee = GetEmployeeID();
- empcontroller.Create(employee);
- IEnumerable<emp> employees = emprepository.GetAllEmployee();
- Assert.IsTrue(employees.Contains(employee));
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- emp GetEmployeeID()
- {
- return GetEmployeeName(1, "ravi", 50000, 10);
- }
- /// <summary>
- ///
- /// </summary>
- [TestMethod]
- public void Create_PostRedirectOnSuccess()
- {
- EmployeeController controller = GetEmployeeController(new InmemoryEmpRepository());
- emp model = GetEmployeeID();
- var result = (RedirectToRouteResult)controller.Create(model);
- Assert.AreEqual("Index", result.RouteValues["action"]);
- }
- /// <summary>
- ///
- /// </summary>
- [TestMethod]
- public void ViewIsNotValid()
- {
- EmployeeController empcontroller = GetEmployeeController(new InmemoryEmpRepository());
- empcontroller.ModelState.AddModelError("", "mock error message");
- emp model = GetEmployeeName(1, "", 0, 0);
- var result = (ViewResult)empcontroller.Create(model);
- Assert.AreEqual("Create", result.ViewName);
- }
- /// <summary>
- ///
- /// </summary>
- [TestMethod]
- public void RepositoryThrowsException()
- {
- // Arrange
- InmemoryEmpRepository emprepository = new InmemoryEmpRepository();
- Exception exception = new Exception();
- emprepository.ExceptionToThrow = exception;
- EmployeeController controller = GetEmployeeController(emprepository);
- emp employee = GetEmployeeID();
- var result = (ViewResult)controller.Create(employee);
- Assert.AreEqual("Create", result.ViewName);
- ModelState modelState = result.ViewData.ModelState[""];
- Assert.IsNotNull(modelState);
- Assert.IsTrue(modelState.Errors.Any());
- Assert.AreEqual(exception, modelState.Errors[0].Exception);
- }
- private class MockHttpContext : HttpContextBase
- {
- private readonly IPrincipal _user = new GenericPrincipal(new GenericIdentity("someUser"), null /* roles */);
- public override IPrincipal User
- {
- get
- {
- return _user;
- }
- set
- {
- base.User = value;
- }
- }
- }
- }
- }

After few seconds, we will see the following screen:

Dinesh KumarPosted Dec 30, 2021, 8:56 AM
Nice share .
Pradeep SahooPosted May 26, 2016, 7:04 AM
Nice share .....
Vignesh ManiPosted May 25, 2016, 8:25 AM
Good one
Hari ShankerPosted May 25, 2016, 4:08 AM
Nice Sharing
Gowtham RajamanickamPosted May 25, 2016, 1:14 AM
Good one..
Munesh SharmaPosted May 25, 2016, 12:26 AM
good one
Thiruppathi RPosted May 24, 2016, 3:56 PM
Nice..
Omid NasriPosted May 24, 2016, 2:53 PM
thx.
Debasis SahaPosted May 24, 2016, 1:47 PM
Good One..g
Sonu ChaudharyPosted May 24, 2016, 12:46 PM
Nice Sharing..
Kuppurasu NagarajPosted May 24, 2016, 12:04 PM
Nice Sharing..