简体   繁体   English

Swift单元测试-等待服务响应

[英]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. 我有2个委托函数,当服务响应到达时会调用它们。

 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? 我该如何指定呢?

A test which depends on an external API is not much of a "unit" test. 依赖于外部API的测试不是“单元”测试。 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. 这样的double可以通过立即调用您的委托方法来提供同步接口。

If you really need to test asynchronous behavior XCTest provides XCTestExpectation for Writing Tests of Asynchronous Operations . 如果确实需要测试异步行为,则XCTest提供XCTestExpectation编写异步操作测试 Most third party testing libraries provide something similar as well. 大多数第三方测试库也提供类似的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM