Home > DeveloperSection > Beginner > Using TDD with in ASP.NET MVC

Using TDD with in ASP.NET MVC


ASP.Net Testing  ASP.NET MVC 
Ratings:
0 Comment(s)
 1731  View(s)
Rate this:

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

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:

 

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:

 

 

When run code analysis

 


Don't want to miss updates? Please click the below button!

Follow MindStick