有些函数向服务器发送请求、获取响应和打印结果。他们总是在iOS应用程序本身工作,但有时(看起来像随机的)在这个应用程序的单元测试中。
主要问题: Xcode没有在单元测试中进入闭包的主体,只是跳过它。
有什么好办法解决的吗?Xcode中问题的图像。
发布于 2015-12-02 21:59:35
最有可能的原因是您的请求的完成闭包没有被执行,因为它们正在执行异步操作,而测试是同步运行的。这意味着当您的网络请求仍在处理时,测试将完成运行。
尝试使用XCTestExpectation
func testIt() {
let expectation = expectationWithDescription("foobar")
// request setup code here...
Alamofire.request(.POST, "...")
.responseJSON { response in
//
// Insert the test assertions here, for example:
//
if let JSON = response.result.value as? [String: AnyObject] {
XCTAssertEqual(JSON["id"], "1")
} else {
XCTFail("Unexpected response")
}
//
// Remember to call this at the end of the closure
//
expectation.fulfill()
}
//
// This will make XCTest wait for up to 10 seconds,
// giving your request expectation time to fulfill
//
waitForExpectationsWithTimeout(10) { error
if let error = error {
XCTFail("Error: \(error.localizedDescription)")
}
}
}https://stackoverflow.com/questions/34038842
复制相似问题