【发布时间】:2015-10-22 06:24:03
【问题描述】:
从 XCTest 文件中,我正在调用 Web 服务调用,并使用“self.waitForExpectationsWithTimeout”API 放置了一个等待块并获取服务响应。
我有几个测试方法可以用这个服务响应来执行。当我将此响应存储在全局变量中并从其他测试函数访问它时,该变量将为零。这里需要做什么?我可以将它作为函数参数传递吗?
let serviceResp :NSDictionary!
func test_One() {
//let expectation: XCTestExpectation = self.expectationWithDescription("HTTP")
datamanager.fetchData() //Web service
self.waitForExpectationsWithTimeout(5, handler: { (error: NSError!) -> Void in
//In 5 seconds, I will get response from service and will be stored in datamanager.response.
self.serviceResp = datamanager.response
})
}
func test_Two() {
//self.serviceResp is coming as nil even after assigning a value to it.
}
谢谢
【问题讨论】:
-
让我们看一些代码! :)
-
添加了高级代码。
-
你在五秒内得到响应,但其他测试函数在此之前运行很长时间!
-
马特不是这样的。 test_two 将仅在 5 秒后执行
-
好吧,也许
dataManager.response是 nil。您达到 5 秒超时的事实表明您的连接可能失败。
标签: ios swift unit-testing xctest xctestexpectation