How to unit test asynchronous APIs?

后端 未结 13 953
孤城傲影
孤城傲影 2020-11-30 17:34

I have installed Google Toolbox for Mac into Xcode and followed the instructions to set up unit testing found here.

It all works great, and I can test my synchronous

13条回答
  •  無奈伤痛
    2020-11-30 18:09

    If you're using a library such as AFNetworking or ASIHTTPRequest and have your requests managed via a NSOperation (or subclass with those libraries) then it's easy to test them against a test/dev server with an NSOperationQueue:

    In test:

    // create request operation
    
    NSOperationQueue* queue = [[NSOperationQueue alloc] init];
    [queue addOperation:request];
    [queue waitUntilAllOperationsAreFinished];
    
    // verify response
    

    This essentially runs a runloop until the operation has completed, allowing all callbacks to occur on background threads as they normally would.

提交回复
热议问题