Dab88
Dab88

Reputation: 199

Swift Unit test - Wait until service response

I'm try make the Unit Test from my app. My app include service integration. I'm implementing an Adapter for this task and I need wait the service response for process the response and then validate the response object.

I have 2 delegate function that are called when the service response arrive.

 func didReceiveAPIResults(#results: AnyObject, path: String, serverTag: String) 


 func didReceiveAPIResultsFailed(#error: NSError, errorObject: AnyObject, path: String, serverTag: String) 

I need process the response in the previous functions and then finish the Unit Test.

How I can specify this?

Upvotes: 1

Views: 1098

Answers (1)

Jonah
Jonah

Reputation: 17958

A test which depends on an external API is not much of a "unit" test. It may be more reliably to implement a test double for your adapter to return a consistent response. Such a double might then be able to provide a synchronous interface by calling your delegate methods immediately.

If you really need to test asynchronous behavior XCTest provides XCTestExpectation for Writing Tests of Asynchronous Operations. Most third party testing libraries provide something similar as well.

Upvotes: -1

Related Questions