gpt4 book ai didi

ios - 为什么异步网络测试在 Objective-C/Swift 中很困难?

转载 作者:行者123 更新时间:2023-11-28 07:05:16 24 4
gpt4 key购买 nike

因此,当我遇到以下情况时,我正在学习更多有关如何测试异步代码的知识:

As soon as a given test method completes, XCTest methods will consider a test to be finished and move onto the next test. That means that any asynchronous code from the previous test will continue to run while the next test is running. Networking code is usually asynchronous, since you don’t want to block the main thread while performing a network fetch. That, coupled with the fact that tests finish when the test method finishes, can make it hard to test networking code."

然而,这在我看来是矛盾的。如果我没理解错的话,他首先说 XCTest 方法将继续运行,即使异步代码正在完成。但是,他随后说测试方法完成时测试结束。但是这两个说法是矛盾的,因为异步代码还在运行,测试没有结束,但是它在串行队列中继续到下一个进程执行。换句话说,是在异步代码完成时完成测试,还是在异步代码仍在运行时继续测试?除此之外,是什么让网络测试如此困难?谢谢。

最佳答案

分解这个语句告诉我们每个测试方法在返回时完成。因为您正在使用异步回调测试网络代码,所以回调可能不会在方法结束之前发生。因此,如果您不注意,测试异步代码可能会被认为是困难的 **

对此我要补充一点,我正在使用简单的调度组来包装代码并等待代码完成。 Here is a great example that I have inspired myself from to test my own code .

关于ios - 为什么异步网络测试在 Objective-C/Swift 中很困难?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com