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














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..