How to unit test asynchronous APIs?

Posted by Ben Clayton on Stack Overflow See other posts from Stack Overflow or by Ben Clayton
Published on 2010-01-29T13:33:59Z Indexed on 2010/04/11 18:33 UTC
Read the original article Hit count: 641

Hi all.
I have installed Google Toolbox for Mac (http://code.google.com/p/google-toolbox-for-mac/) into Xcode and followed the instructions to set up unit testing found here (http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting).

It all works great, and I can test my synchronous methods on all my objects absolutely fine. However, most of the complex APIs I actually want to test return results asynchronously via calling a method on a delegate - for example a call to a file download and update system will return immediately and then run a -fileDownloadDidComplete: method when the file finishes downloading.

How would I test this as a unit test?

It seems like I'd want to the testDownload function, or at least the test framework to 'wait' for fileDownloadDidComplete: method to run.

Any ideas much appreciated!

© Stack Overflow or respective owner

Related posts about iphone

Related posts about unit-testing