Update Answer
The original answer was quite an old one, before async
and await
were prevalent. I'd now recommend using them, by writing something like:
[TestMethod]
public async Task RunTest()
{
var result = await doAsyncStuff();
// Expectations
}
There's a good article that covers this in depth Async Programming : Unit Testing Asynchronous Code
Old Answer
I would tend to something simple like using a polling loop and checking a flag that would be set in the async code or you could use reset events. A simple example using threads:
[TestMethod]
public void RunTest()
{
ManualResetEvent done = new ManualResetEvent(false);
Thread thread = new Thread(delegate() {
// Do some stuff
done.Set();
});
thread.Start();
done.WaitOne();
}
You'll need to think about exceptions and use a try/finally and report the errors to do this properly, but you get the idea. This method however might not suit if you're doing lots of async stuff over and over, unless you fancy pushing this into a reusable method.