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

时间:2022-06-27 16:20:33

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.

我尝试从我的应用程序进行单元测试。我的应用包括服务集成。我正在为此任务实现一个Adapter,我需要等待服务响应来处理响应,然后验证响应对象。

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?

我怎么指定这个?

1 个解决方案

#1


0  

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.

依赖于外部API的测试并不是一个“单元”测试。为适配器实现测试双精度以返回一致的响应可能更可靠。这样的double可以通过立即调用您的委托方法来提供同步接口。

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.

如果您确实需要测试异步行为,XCTest会提供XCTestExpectation来编写异步操作测试。大多数第三方测试库也提供类似的东西。

#1


0  

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.

依赖于外部API的测试并不是一个“单元”测试。为适配器实现测试双精度以返回一致的响应可能更可靠。这样的double可以通过立即调用您的委托方法来提供同步接口。

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.

如果您确实需要测试异步行为,XCTest会提供XCTestExpectation来编写异步操作测试。大多数第三方测试库也提供类似的东西。