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

标签 ios swift asynchronous concurrency grand-central-dispatch

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

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/

相关文章:

ios - xcode 4 仅在设备上复制一次资源

swift - FlatMapLatest 跳过触发,直到最新的可观察完成

ios - SmartView SDK Samsung/Tizen - 内部服务器错误 500

c# - 捕获异步 lambda 异常

c# - 为什么 C# 锁不应该用于长时间操作?

java - Fragment 中 AsyncTask 中的 ProgressDialog 崩溃

iphone - InAppSettingsKit UIWebView

ios - 使用未声明的类型 UNAuthorizationOptions

swift - 使用 NSMutableString 时出现运行时错误

ios - 使用 firebase 检索图像给出 : EXC_BAD_ACCESS (code=1, 地址=0x10)