articles

Home / DeveloperSection / Articles / Using TDD with in ASP.NET MVC

Using TDD with in ASP.NET MVC

Anonymous User6863 20-Feb-2015

Hi everyone in this article I’m explaining about Test Driven Development in mvc.

Description:

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. 

Get Started:

Open visual studio >> File >> New Project >> ASP.NET MVC 4 Web Application

Using TDD with in ASP.NET MVC

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

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

 

Using TDD with in ASP.NET MVC

 

Now add interface:

 

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

 

BookRepository:
    publicclassBookRepository:IBookRepository
    {
        MyDBEntities db = null;
        public BookRepository(MyDBEntities db)
        {
            this.db = db;
        }
        publicList<Book> GetAllBook()
        {
            return db.Book.ToList();
        }
        publicBook GetBookById(int id)
        {
            return db.Book.FirstOrDefault(m => m.ID == id);
        }
        publicvoid AddBook(Book model)
        {
            db.Entry(model).State = System.Data.Entity.EntityState.Added;
        }
        publicvoid UpdateBook(Book model)
        {
            db.Entry(model).State = System.Data.Entity.EntityState.Modified;
        }
        publicvoid DeleteBook(int id)
        {
            Book model = db.Book.FirstOrDefault(m => m.ID == id);
            db.Entry(model).State = System.Data.Entity.EntityState.Deleted;
        }
        publicvoid Save()
        {
            db.SaveChanges();
        }
    }

 

HomeController:
publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        BookRepository bookrepo;
 
        public HomeController()
        {
            this.bookrepo = newBookRepository(newMyDBEntities());
        }
 
        publicActionResult 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

 

When run code analysis

 

Using TDD with in ASP.NET MVC


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By