Test Driven Development(TDD) in ASP.NET MVC

Test Driven Development in short TDD is one of the software development approach where we write all possible test cases to fulfil our required functionality and then develop actual functional code to satisfy all the test cases. Test Driven Development is reverse of normal software development. In normal development first we develop actual functional code then write the test cases but in TDD it is reverse.

Today we discuss about how to implement Test Driven Development process in Asp.Net MVC. Asp.Net MVC supports Unit Testing in a well manner because in Asp.Net MVC all code is divided into separate parts so we can easily write test cases for each part(Model, Controller and View). Today we discuss about how to do TDD in Asp.Net MVC to test Model class methods. 

Open Microsoft Visual Studio 2013 => Create New Project for Asp.Net MVC 4 Web Application and name it as TDDAspNetMVC, Click on OK. 

In next window select "Create a unit test project" and name it as TDDAspNetMVC.Tests, Click OK button as shown below. 

          

 Add class to Models folder and it as Discounts.cs. Add GetDiscountAmount() method to Discounts class as shown below. 

using System;

namespace TDDAspNetMVC.Models
{
    public classDiscounts
    {
        public double GetDiscountAmount(double dActualAmmount)
        {
            throw new NotImplementedException();
        }
    }
}

As explained we didn't add the implementation to GetDiscountAmount() method now as part of TDD(Test Driven Development).

 

Add Basic Unit Test class to TDDAspNetMVC.Tests project and name it as DiscountsTest.cs. Before going to write test case for our code, add reference to TDDAspNetMVC project for the TDDAspNetMVC.Tests project. Right click on TDDAspNetMVC.Tests project and select Add Reference, add reference to TDDAspNetMVC as shown below. 

Now write test case for GetDiscountAmount() method as shown below. 

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TDDAspNetMVC.Models; 

namespace TDDAspNetMVC.Tests
{
    [TestClass]
    public classDiscountsTest
    {
        [TestMethod]
        public void DiscountTestMethod()
        {
            Discounts obj = newDiscounts();

            double dAmount = 20000; 
            double iDiscountAmount = obj.GetDiscountAmount(dAmount); 

            Assert.AreEqual(dAmount * 0.30, iDiscountAmount);
        }
    }
}

As shown above DiscountsTest class is marked with TestClass attribute which makes normal class as Unit Test class and DiscountTestMethod() class marked with TestMethod attribute. The TestClass and TestMethod attributes derived from Microsoft.VisualStudio.TestTools.UnitTesting class.

 

As shown above we created the object for Discounts class of Models folder and calling the GetDiscountAmount() method by passing some value. As a business rule if the amount is greater than or equal to 20000, discount should be 30%. So we are testing the functionality with Assert class AreEqual method. 

Now run the test cases by going to TEST => Run => All Tests as shown below. 

Once you Run the Test Cases it shows the results as shown below. 

As shown above it shows the result as Test Failed, throws the exception as NotImplemented because as of now we didn't added any logic. Add logic to GetDiscountAmount() method as shown below in Discounts.cs class. 

using System;

namespace TDDAspNetMVC.Models
{
    public classDiscounts
    {
        public double GetDiscountAmount(double dActualAmmount)
        {
            if (dActualAmmount < 20000)
            {
                return dActualAmmount * 0.10;
            }
            else
            {
                return dActualAmmount * 0.30;
            }
        }
    }
}

As we discussed we are providing the 10% discount if the actual amount is less than 20000 and providing 30% if the actual amount is greater than or equal to 20000. Now run the test cases and the output is as shown below.

 

It shows the result as Test Passed. Let's change the logic for GetDiscountAmount() method as shown below. Providing the 30% discount if the amount is greater or equal to 30000 which is the violation of our business rules. 

using System; 

namespace TDDAspNetMVC.Models
{
    public classDiscounts
    {
        public double GetDiscountAmount(double dActualAmmount)
        {
            if (dActualAmmount < 30000)
            {
                return dActualAmmount * 0.10;
            }
            else
            {
                return dActualAmmount * 0.30;
            }
        }
    }
}

Again Run the Test cases and result is as shown below. 

 It shows the result as Test Failed and provides the message as Assert.AreEqual failed because one of the business rule is failed. 

                                                                                                                    TDDAspNetMVC.zip (7.16 mb)