NUnit testing asynchronous events

Posted by Marcus Hammarberg on October 25, 2006
Stats
In my current project we are using a some asynchronous webservice method calls. The actual call are in turn encapsulated in a layer that is used by the GUI, the "ClientCommand"-layer.

However when writing tests for these method we ran into trouble.... First the NUnit-framework is not very good at handling events at all, but by using this excellent article by Peter Provost i got a nice, compact way of doing the tests.

Then it was the whole asynchronous thing, which really was hard to get by. The solution given above worked out fine as long as not any assertions went threw exceptions - but what is the use of that?

So the simple solution to the problem was to simply wait for the event to fire and be handled. This was solved by setting a bool variable (bEventHandled) and not continue with the assertions until that bool was true.

The event handling delegate sets the method variables to the return values from the event and finally switch the EventHandled-bool to true so that the assertions can be run.

Here's the code for a example:
[Test(Description = "Demo Async call")]
public void DemoAsyncTest()
{
// Create the variables to hold result
EpasAsyncUserStateArgs argsActual = null;
DataSet dsActual = null;
bool bEventHandled = false;

// Create the object to test
ClaimsReportClientCommand c = new ClaimsReportClientCommand();

// Subscribe to the event with a delegate
c.LoadClaimInfoCompleted += delegate
(EpasAsyncUserStateArgs args, DataSet dsResult)
{
// Set the method variable

// This can be done because of the anonymous delegate
argsActual = args;
dsActual = dsResult;

// set the event handled
bEventHandled = true;
};

// Do call that generates the event
c.LoadClaimInfoById(CLAIMREPORT_ID_EXSTING, "se");

// Wait until the event is handled
while (!bEventHandled)
{
System.Threading.Thread.Sleep(50);
}

// Result recived - do assertions (whatever...)
Assert.IsNotNull(argsActual);
Assert.IsNotNull(dsActual);
}

So, as you can see, most of it is a complete rip of from Peters article. The delegate-stuff is the key for this to work. If the eventhandler is a standard eventhandler it cannot set the variables in the method for the later assertions. Also the whole testcode is in one method which also is good.

I realise that it probably could be another, better and slicker way of testing asynchronous webservice methods, but I haven't found it. After quite a while of googling i must add. If you know one, please let me know too :-)

Published by Marcus Hammarberg on Last updated