为什么是异步的网络测试很难在Objective-C /斯威夫特? [英] Why is asynchronous network testing difficult in Objective-C/Swift?

查看:138
本文介绍了为什么是异步的网络测试很难在Objective-C /斯威夫特?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我更多地了解如何测试异步code,当我遇到以下内容:

So I'm learning more about how to test asynchronous code when I come across the following:

只要给定的测试方法完成,XCTest方法会考虑
  一个测试来完成,并进入下一个测试。这意味着,任何
  从previous测试异步code将继续运行,而
  接下来的测试运行。网络code是通常是异步的,
  因为你不希望阻止主线程在执行
  网络获取。也就是说,加上一个事实,即测试结束时,
  测试方法完成,可以使它很难测试组网code。

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方法将继续即使异步code正在整理运行。不过,他接着说,测试完成时,测试方法完成。但是这两个语句抵触,作为供试没有完成,因为异步code仍在运行,但它在串行队列前进到下一个处理中执行。换句话说,做测试时完成异步code结束,或者说测试继续,而异步code仍在运行?最重要的是,是什么让网络测试这么难吗?谢谢你。

However, this is contradicting in my mind. If I'm understanding correctly, he says first that XCTest methods will continue to run even if asynchronous code is finishing. However, he then says that tests finish when the test method finishes. But these two statements contradict, as the test is not finished because asynchronous code is still running, yet it proceeds in the serial queue to the next process to execute. In other words, do the tests finish when the async code finishes, or do the tests continue while the async code is still running? On top of that, what makes network testing so difficult? Thank you.

推荐答案

打破这句话告诉我们,当它返回每个测试方法完成。因为您正在使用异步回调测试网络code,回调可能不会发生之前的方法结束。因此测试异步code可以被认为是潜在的困难**,如果你不注意**

Breaking down this statement tells us that each test method finishes when it returns. Because you are testing network code with an async callback, the callback may not happen BEFORE the method ends. Therefore testing async code can be considered potentially difficult ** if you don't pay attention **

要这个,我会补充一点,我现在用的是简单的调度组包code和等待code来完成。 <一href=\"http://commandshift.co.uk/blog/2014/03/19/using-dispatch-groups-to-wait-for-multiple-web-services/\"相对=nofollow>这里是一个很好的例子,我有启发自己从测试我自己的code 。

To this I will add that I am using the simple dispatch group to wrap code and wait for the code to finish. Here is a great example that I have inspired myself from to test my own code.

这篇关于为什么是异步的网络测试很难在Objective-C /斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆