Test-Driven Development in ASP.NET MVC
Mar 02, 2021 07:30 0 Comments ASP.net MVC PARTH

    Test-Driven Development in ASP.NET MVC

 

We know that ASP.NET MVC has an advantage towards testability, which makes it fit for Test-Driven Development (TDD) in a simple and easy manner. Since an MVC application is developed in a loosely coupled manner, we can test the independent parts of the application very easily and effectively.

In this blog, I will brief you about the concept of Test-Driven Development (TDD) in ASP.NET MVC. Here, I will be using NUnit unit test framework.

 

What is Test-Driven Development ?

TDD is a design approach, where we write a test case, before we write the complete code to fulfill all the requirements. It is also known as Red-Green-Refactor style development.

The following steps govern the workflow of TDD –

1. Identification on requirements

2. Writing an automated test

3. Running all the tests and making sure newly created test case fails (RED)

4. Writing some code to fix the failed test case

5. Running all the tests again to make sure all the test cases pass (GREEN)

6. This is followed by Refactoring of the code

7. We repeat the above steps when a new requirement comes up in future

 

                               

 

What is NUnit ?

NUnit is a free, open-source tool for testing the .NET/MVC applications by automating the unit testing process. NUnit can be downloaded from NUnit site ( https://nunit.org/download/ )

                                       

Let’s understand this with an example –

We are testing a sample application which is a simple command-line application having a single class, Man. The Man class has three properties: Age, Name, and TotalBalance, along with a method, BuyingACar, which debits amount from TotalBalance.

 

class Man

{

   string name;

   int age;

   float totalBalance;

 

   Man()

   {

      Name = “”;

      age = 0;

      totalBalance = 0;

   }  

  

   public Man(string name , int age)

   {

      name = name;

      age = age;

      totalBalance = 100000;

   }

 

   public int Age

   {

      get{return age;}

   }

  

   public string Name

   {

      get{return name;}

   }

  

   public float TotalBalance

   {

      get {return totalBalance;}

   }

 

   public void BuyingACar (float cost)

   {

      totalBalance = totalBalance - cost;

   }

}

 

Let’s see the test class containing the methods to test the class using NUnit test framework –

 

using NUnit.Framework;

[TestFixture]

public class TestClass

{

   Man mTest;

   public TestClass()

   {

      // Add logic here

   }

 

   [SetUp]public void Init()

   {

      mTest = new Man("Parth", "Jolly", 29);

   }

 

   [Test]public void IsNameParthJolly()

   {

      Assert.IsFalse("Parth Jolly" == mTest.Name);

   }

 

   [Test]public void IsAgeEqual()

   {

      Assert.AreEqual(29, mTest.Age);

   }

 

   [Test]public void BalanceBeforeACarIsBrought()

   {

      Assert.AreEqual(100000, mTest.TotalBalance);

   }

}

 

Now, we run the test cases in NUnit testing framework –

You can see all the test cases got passed (GREEN).

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect