Being a .Net developer I never had to struggle in writing quality code prior to MVC era and mainly because I was heavily dependent on Visual Studio IDE and MSDN. I was writing code for all layers for given functionality and my UI team member was helping me in adjusting UI (mainly HTML). However, as technology moved to next level and Microsoft introduces MVC framework, demand for modular application increased and we were forced to opt for MVC framework. Since, separate team was working on all layers though focus was same yet issues were different. UI team was working on developing better user interface using some most popular UI framework like bootstrap and our job was to write better models and controllers which follow industries best practices. Our challenge was how to ensure that our code is in-line with business requirement in absence of UI? Previously, we were testing our code (functionality) starting from UI till database but now different team was working on UI and same was not available prior to middle layer coding begin.
While exploring possible solutions to test our controller, we can to know about test driven development (also called Test first approach). TDD was introduced long back in 1999 as part of XP (eXtreme Programming) by the group of developers and basic philosophy was to “Before you write code, think about what it will do”. Let me say that it was not difficult when I started reading and practicing same because this is what I was doing during studying COBOL so simply loved it. TDD is now part of Scrum engineering practices as well. Scrum Alliance recently introduces another certification program called CSD (certified Scrum Developer) and TDD is part of CSD. I will write more about CSD in my next article.
Question is what all needed for writing code using TDD philosophy? Nothing more than a Unit Test framework and skills for writing good unit test cases. There are many unit test framework but my personal favorite is NUnit test framework but how to write better test cases. If your unit test is following below characteristics than you are writing better unit test.
Unit test characteristics – Isolated from the other code, Isolated from the other developers, Targeted, Repeatable and Predictable.
TDD as stated above follow the principle of writing test 1st then write code so basically test will fail in 1st attempt but you should write code only that much needed pass the test. Below is cycle of Test Driven Development.
Following is code example for testing controller for managing phones number project using MVC 4.0 and NUnit.
Step 1 – Open a new MVC 4.0 project
Step 2 – Add NUnit reference (you can download NUnit from http://www.nunit.org)
Step 3 – Add a class to write test (MyPhoneBookTest.cs) and write below test
public class MyPhoneBookTest
public void CheckWelcomeMessage()
HomeController homeController = new HomeController();
ViewResult result = homeController.Index() as ViewResult;
Assert.AreEqual(“Welcome to My Phone Book.”, result.ViewBag.Message);
Step 4 – Run your test using NUnit GUI and you will get below result showing test has failed
Step 5 – replace current message with expected message in HomeController.cs file
public ActionResult Index()
ViewBag.Message = “Modify this template to jump-start your ASP.NET MVC application.”;
With ViewBag.Message = “Welcome to My Phone Book.”;
Step 6 – Re-run your test using NUnit GUI and you will get GREEN this time
Time to add more test based on business requirement!!!
What next – If you are interested in knowing more about TDD and CSD then please reach out to us on DISCUSSION FORUM
No Trainings found