Testable application should be developed in a loosely coupled manner so that you can test the independent parts of the application. Asp.net MVC support this feature so i suggest you can use this framework if you want to use TDD.
Here are a few techniques that you can use to get you started. If you are unfamiliar with TDD, what I’m about to say may sound a little strange to you. A lot of people have spent a lot of time telling us that we should carefully design our classes, code them up and then test them. But in test drive based on completely different approach. Instead of designing a module, then coding it and then testing it, you turn the process around and do the testing first. To put it another way, you don’t write a single line of production code until you have a test that fails. The typical programming sequence is something like this:
-
Add a test
-
Run all tests and watch the new one fail (and the rest succeed)
-
Modify our code to make the test succeed
-
Run all tests and watch them succeed
-
Refactor, if necessary
-
Run tests to ensure everything still works
-
Repeat
I will explain with simple example using .net coding. First create the .net main project with one testing project.
Add the ICalculator interface to your project,
public interface ICalculator { double Total { get; } double Add(double value); double Subtract(double value); double Clear(); }
Now create the Calculator class and implement it from ICalculator as shown below,
using System; using System.Collections.Generic; using System.Text; using TestDriven.Calculator.Core; namespace TestDriven.Calculator { public class Calculator:ICalculator { #region ICalculator Members public double Total { get { throw new Exception("The method or operation is not implemented."); } } public double Add(double value) { throw new Exception("The method or operation is not implemented."); } public double Subtract(double value) { throw new Exception("The method or operation is not implemented."); } public double Clear() { throw new Exception("The method or operation is not implemented."); } #endregion } }
Now your core proejct is ready with Calculator stuff. But still you have not started your coding yet… Now you can add test case to test the Calculator implementation. Create the .net test project and add your main project reference to your test project. We will add the empty class called CalculatorTest.cs to test project.
Now add one test method called CalculatorTest as shown below in created test project.
[TestMethod] public void CalculatorTest() { ICalculator calc = new Calculator(); Assert.IsTrue(null != calc, "Construction failed"); Assert.IsTrue(0.0D == calc.Total, "Value should initially be 0.0"); }
Run the above test case will fails because we have not implemented the functionality in Calculator Class as shown above. Now its time to implement the required functionality as shown below in main project.
public class Calculator:ICalculator { public Calculator() { m_total = 0.0; } private double m_total; #region ICalculator Members public double Total { get { return m_total; } } public double Add(double value) { throw new Exception("The method or operation is not implemented."); } public double Subtract(double value) { throw new Exception("The method or operation is not implemented."); } public double Clear() { throw new Exception("The method or operation is not implemented."); } #endregion }
Now compile and run the test case again, your test case should run successfully. Now we are down with first iteration of the test driven development cycle.
Second iteration of the test driven development cycle
Well now we will start writing the test case for Calculator add and subtract method.
[TestMethod] public void CalculatorTest() { ICalculator calc; double expected, actual; calc = new Calculator(); expected = -1.0; actual = calc.Subtract(1.0); Assert.AreEqual(expected, actual, "0 - 1 should be -1"); Assert.AreEqual(expected, calc.Total, "total is incorrect"); expected = -2.5; actual = calc.Subtract(1.5); Assert.AreEqual(expected, actual, "-1 -1.5 should be -2.5"); Assert.AreEqual(expected, calc.Total, "total is incorrect"); }
We compile and run the tests and they will fail. Next we will update the calculator class as shown below,
public class Calculator:ICalculator { public Calculator() { m_total = 0.0; } private double m_total; #region ICalculator Members public double Total { get { return m_total; } } public double Add(double value) { m_total += value; return m_total; } public double Subtract(double value) { m_total -= value; return m_total; } public double Clear() { throw new Exception("The method or operation is not implemented."); } #endregion }
Now we have implemented Add and Subtract functionality. Compile the project and run the test case again, Now test case should pass.
This is the point where we would check to see if there are any places where we could refactor our code to make it more efficient. For example, even though our current implementation is more than adequate, let’s say we wanted to change the way we implemented the Subtract() method. We will update the code, compile and run our tests to be sure we didn’t break anything.
public double Subtract(double value) { return Add(-1.0 * value); }
So now we are done with our second iteration of the test driven development cycle.
Now you should start the third iteration of TDD for Clear() functionality. This is the excersice for your 🙂
In my next blog i will cover how to use TDD effectively in .net MVC. If you have any query just comment i will try to answer.
Similar Topics:
- Implementing Factory Pattern in .net
- Implementing Unit of Work Pattern in .net
- Implementing Abstract Factory Pattern in .net
- Dependency Injection (DI) Pattern in asp.net
- Introduction To Model-View-ViewModel (MVVM) Pattern
- Repository Pattern in .net
- Visual Studio 2012 interesting features
- Outlook Web Access corrupts HTML attachments