Click here to Skip to main content
15,881,559 members
Articles / Web Development / ASP.NET

Creating Unit Testable Applications in ASP.NET MVC - A Beginner's Tutorial

Rate me:
Please Sign up or sign in to vote.
4.94/5 (28 votes)
16 Apr 2013CPOL6 min read 116.5K   3.3K   78  
In this article we will talk about creating unit testable applications using ASP.NET MVC.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TddMvcSample.Models;
using TddMvcTestProject.Repositories;
using System.Web.Mvc;

namespace TddMvcSample.Controllers.Test
{
    [TestClass]
    public class BooksControllerTest
    {
        Book book1 = null;
        Book book2 = null;
        Book book3 = null;
        Book book4 = null;
        Book book5 = null;

        List<Book> books = null;
        DummyBooksRepository booksRepo = null;
        UnitOfWork uow = null;
        BooksController controller = null;

        public BooksControllerTest()
        {
            // Lets create some sample books
            book1 = new Book { ID = 1, BookName = "test1", AuthorName = "test1", ISBN = "NA" };
            book2 = new Book { ID = 2, BookName = "test2", AuthorName = "test2", ISBN = "NA" };
            book3 = new Book { ID = 3, BookName = "test3", AuthorName = "test3", ISBN = "NA" };
            book4 = new Book { ID = 4, BookName = "test4", AuthorName = "test4", ISBN = "NA" };
            book5 = new Book { ID = 5, BookName = "test5", AuthorName = "test5", ISBN = "NA" };

            books = new List<Book>
            {
                book1,
                book2,
                book3,
                book4
            };


            // Lets create our dummy repository
            booksRepo = new DummyBooksRepository(books);

            // Let us now create the Unit of work with our dummy repository
            uow = new UnitOfWork(booksRepo);

            // Now lets create the BooksController object to test and pass our unit of work
            controller = new BooksController(uow);
        }
        
        [TestMethod]
        public void Index()
        {
            // Lets call the action method now
            ViewResult result = controller.Index() as ViewResult;

            // Now lets evrify whether the result contains our book entries or not
            var model = (List<Book>)result.ViewData.Model;

            CollectionAssert.Contains(model, book1);
            CollectionAssert.Contains(model, book2);
            CollectionAssert.Contains(model, book3);
            CollectionAssert.Contains(model, book4);

            // Uncomment the below line and the test will start failing
            // CollectionAssert.Contains(model, book5);
        }

        [TestMethod]
        public void Details()
        {
            // Lets call the action method now
            ViewResult result = controller.Details(1) as ViewResult;

            // Now lets evrify whether the result contains our book
            Assert.AreEqual(result.Model, book1);
        }

        [TestMethod]
        public void Create()
        {   
            // Lets create a valid book objct to add into
            Book newBook = new Book { ID = 7, BookName = "new", AuthorName = "new", ISBN = "NA" };

            // Lets call the action method now
            controller.Create(newBook);

            // get the list of books
            List<Book> books = booksRepo.GetAllBooks();

            CollectionAssert.Contains(books, newBook);
        }

        [TestMethod]
        public void Edit()
        {
            // Lets create a valid book objct to add into
            Book editedBook = new Book { ID = 1, BookName = "new", AuthorName = "new", ISBN = "NA" };

            // Lets call the action method now
            controller.Edit(editedBook);

            // get the list of books
            List<Book> books = booksRepo.GetAllBooks();

            CollectionAssert.Contains(books, editedBook);
        }

        [TestMethod]
        public void Delete()
        {
            // Lets call the action method now
            controller.Delete(1);

            // get the list of books
            List<Book> books = booksRepo.GetAllBooks();

            CollectionAssert.DoesNotContain(books, book1);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions