---
title: "Using TDD with in ASP.NET MVC"  
description: "Hi everyone in this article I’m explaining about Test Driven Development in mvc."  
author: "Anonymous User"  
published: 2015-02-20  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1597/using-tdd-with-in-asp-dot-net-mvc  
category: "asp.net"  
tags: ["testing", "asp.net mvc"]  
reading_time: 3 minutes  

---

# Using TDD with in ASP.NET MVC

Hi everyone in this article I’m explaining about Test Driven Development in mvc.\

##### Description:

[Test Driven Development](https://www.mindstick.com/articles/1593/introduction-of-test-driven-development-tdd) (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](https://www.mindstick.com/articles/310501/what-is-scrum-in-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](https://www.mindstick.com/articles/23249/rdlc-report-in-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](https://www.mindstick.com/forum/12890/how-can-asp-dot-net-mvc-controller-return-an-image) 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.

##### Get Started:

Open visual studio >> File >> New Project >> ASP.NET MVC 4 [Web Application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about)

![Using TDD with in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/68d45572-ea62-4d71-890c-9014a5460ca0/images/8227bc95-1f1b-4773-acf5-18e52b5e4ba8.png)

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.

![Using TDD with in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/68d45572-ea62-4d71-890c-9014a5460ca0/images/a7a4eada-7488-44de-a950-edb1c375910a.png)

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](https://www.mindstick.com/articles/12249/crud-operation-in-border-layout-on-grid) and use repository patter if you learn more about crud operation using repository you can read my previous post [Crud Operations](https://www.mindstick.com/forum/161461/how-do-you-perform-crud-operations-in-mongodb-using-mongoose) Using Generic [Repository Pattern](https://www.mindstick.com/articles/1565/crud-operations-using-generic-repository-pattern-in-asp-dot-net-mvc-4) in ASP.NET MVC 4.

##### Now create database and table like this

![Using TDD with in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/68d45572-ea62-4d71-890c-9014a5460ca0/images/89e64226-4f39-4631-ad9d-4001ffe328b1.png)

##### Now add interface:

##### IBookRepository:

```
    interface IBookRepository    {        List<Book> GetAllBook();        Book GetBookById(int id);        void AddBook(Book model);        void UpdateBook(Book model);        void DeleteBook(int id);        void Save();   
}
```

##### BookRepository:

```
    public class BookRepository:IBookRepository    {        MyDBEntities db = null;        public BookRepository(MyDBEntities db)        {            this.db = db;        }        public List<Book> GetAllBook()        {            return db.Book.ToList();        }        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()        {            db.SaveChanges();        }   
}
```

##### HomeController:

```
public class HomeController : Controller    {        //        // GET: /Home/         BookRepository bookrepo;         public HomeController()        {            this.bookrepo = new BookRepository(new MyDBEntities());        }         public ActionResult Index()        {            return View(bookrepo.GetAllBook());        }    
}
```

Index.cshtml:

```
@model IList<TDDWithMVC.Models.Book>@{    ViewBag.Title = "Index";}  <table>    <tr>        <th>ID</th>        <th>Book Name</th>        <th>Author Name</th>        <td>ISBN</td>    </tr>    @foreach (var item in Model)    {        <tr>            <td>@item.ID</td>            <td>@item.BookName</td>            <td>@item.AuthorName</td>            <td>@item.ISBN</td>        </tr>    }</table>
```

##### Now right click on MvcContact.Test project and run application

##### Output:

![Using TDD with in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/68d45572-ea62-4d71-890c-9014a5460ca0/images/906ef482-eb02-4bee-ae01-0c2002db4b61.png)

##### When run code analysis

![Using TDD with in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/68d45572-ea62-4d71-890c-9014a5460ca0/images/3aec6148-9c0d-4b2b-b803-1b5ffdc39eb3.png)

---

Original Source: https://www.mindstick.com/articles/1597/using-tdd-with-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
