In ASE Lab, we always try to follow Test Driven Development (TDD) approach. As part of my research, I have been developing TouchToolkit for sometime now. Its aim is to simplify the multi-touch application developer process from a number of key areas including cross-device support, DSL for defining new gestures, automated acceptance test support for touch interactions, tools for visual feedbacks, and so on.
Recently, I started developing the storage module for TouchToolkit. It will allow developers to record user interactions from live applications that later can be used for debugging, automated tests, live demo, tutorials, etc. Since the toolkit can be used in applications built on Silverlight, WPF or XNA, I needed to consider different storage medium (i.e. isolated storage for Silverlight, local file system for WPF) for local cache. But the challenge I am facing here is to write UnitTests for the remote-storage module that uses asynchronous web service calls.
Now, lets think about a scenario where I need to test an asynchronous get() method. First I need to save an unique value via another asynchronous call and wait until it completes the process. Then invoke the actual get() method and wait until the data is received from server to validate with the expected values. While discussing this issue in lunch break, the first suggestion I got was to wait for callback for x amount of time using Thread.Sleep(). Now, obviously this is can give false alerts and can significantly increase the time to run the tests.
The interesting thing is, it is not necessary to have the validation codes (i.e. Assert) inside the test method. It can be inside the callback method too. And using the Lamda expression we can have these callbacks as inline functions. The Visual Studio Unit Test runner is actually smart enough to figure that out. So, here is my final code for testing one of the get() methods:
public void GetGestureTest()
// Data set
string userName = GetUniqueGestureName(),
gestureName = GetUniqueGestureName(),
projectName = GetUniqueProjectName(),
gestureData = "GestureData-" + Guid.NewGuid();
StorageManager target = new StorageManager(userName);
// 1. Save the test data in storage
target.SaveGesture(projectName, gestureName, gestureData, (errorOnSave) =>
if (errorOnSave == null)
// 2. Get the data from storage and validate
target.GetGesture(projectName, gestureName, (projName, gesName, gesData, errorOnGet) =>
if (errorOnGet == null)
// 4. Check the validation result
Assert.AreSame(projectName, projName, "Actual project name doesn't match with the expected value");
Assert.AreSame(gestureName, gesName, "Actual gesture name doesn't match with the expected value");
Assert.AreSame(gestureData, gesData, "Actual gesture data doesn't match with the expected value");