Hi everyone in this article I’m explaining about Test Driven Development in mvc.
Test Driven Development (TDD) is gaining momentum with each passing day. The reason for this is that with TDD we can not only built robust applications but also have a proof that the application is working correctly from a functional perspective by having successfully unit test cases on our modules. Also, the projects following agile methodology find TDD very useful because the major challenge in agile is to define when our "done" is done. If we follow TDD approach then we know that when all our test cases are passed, we are done.
ASP.NET MVC application provide a very good support for TDD because we can write unit test directly against our controllers without needing any web server. Doing so we can be sure that the controllers are working correctly from a functionality perspective.
This approach shows you how to develop an ASP.NET MVC application in Visual Studio using the test-driven development (TDD) approach. MVC was designed to enable testability without requiring dependencies on a Web server (IIS), on a database, or on external classes.
In this approach you will create test for an mvc controller before you implement the controller functionality you can write tests before you have a controller. The advantage is that compiler errors in your unit tests are then a first-level form of unit-test failure. The emphasis is on how to design the intent of the controller by writing unit tests before implementing the controller itself, which is an important aspect of the TDD philosophy.
Open visual studio >> File >> New Project >> ASP.NET MVC 4 Web Application
Give the application name “MvcContact” and click ok. After click ok dialog box displayed make sure create a unit test project is selected and then click ok.
Visual Studio creates a solution that contains two projects, one named MvcContacts and one named MvcContacts.Tests.
Now our project is created successfully. Now I have make simple crud operation and use repository patter if you learn more about crud operation using repository you can read my previous post Crud Operations Using Generic Repository Pattern in ASP.NET MVC 4.
Now create database and table like this
Now add interface:
Book GetBookById(int id);
void AddBook(Book model);
void UpdateBook(Book model);
void DeleteBook(int id);
public class BookRepository:IBookRepository
MyDBEntities db = null;
public BookRepository(MyDBEntities db)
this.db = db;
public List<Book> GetAllBook()
public Book GetBookById(int id)
return db.Book.FirstOrDefault(m => m.ID == id);
public void AddBook(Book model)
db.Entry(model).State = System.Data.Entity.EntityState.Added;
public void UpdateBook(Book model)
db.Entry(model).State = System.Data.Entity.EntityState.Modified;
public void DeleteBook(int id)
Book model = db.Book.FirstOrDefault(m => m.ID == id);
db.Entry(model).State = System.Data.Entity.EntityState.Deleted;
public void Save()
public class HomeController : Controller
// GET: /Home/
this.bookrepo = new BookRepository(new MyDBEntities());
public ActionResult Index()
ViewBag.Title = "Index";
@foreach (var item in Model)
Now right click on MvcContact.Test project and run application
When run code analysis