For the More Curious: Asynchronous Testing

Suppose that you want to test a method like the following:

f​u​n​c​ ​d​o​W​o​r​k​I​n​B​a​c​k​g​r​o​u​n​d​W​i​t​h​C​o​m​p​l​e​t​i​o​n​(​c​o​m​p​l​e​t​i​o​n​H​a​n​d​l​e​r​:​ ​(​B​o​o​l​,​ ​N​S​E​r​r​o​r​?​)​ ​-​>​ ​(​V​o​i​d​)​)​

This method takes a completion handler which returns asynchronously, sometime after the function returns. You would like to test that this function does eventually call its completion handler, and that when the completion handler is called, it is passed true for its first argument, and nil for its second. The techniques that have been discussed so far in the chapter do not allow for testing this method. For testing an asynchronous method, XCTest provides XCTestExpectation ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.