XCTest waitForExpectationsWithTimeout:handler:的文档说明了这一点
只有一个-waitForExpectationsWithTimeout:handler:可以在任何给定时间处于活动状态,但是{expected - > wait}的多个离散序列可以链接在一起。
但是,我不知道如何实现它,也找不到任何例子。我正在开发一个类,首先需要找到所有可用的串口,选择正确的端口,然后连接到连接到该端口的设备。所以,我正在处理至少两个期望,XCTestExpectation * expectationAllAvailablePorts和* expectationConnectedToDevice。我怎么把这两个连在一起?
迅速
let expectation1 = //your definition
let expectation2 = //your definition
let result = XCTWaiter().wait(for: [expectation1, expectation2], timeout: 10, enforceOrder: true)
if result == .completed {
//all expectations completed in order
}
迅速
let expectation1 = //your definition
let expectation2 = //your definition
let result = XCTWaiter().wait(for: [expectation1, expectation2], timeout: 10, enforceOrder: true)
if result == .completed {
//all expectations completed in order
}
我做了以下工作。
expectation = [self expectationWithDescription:@"Testing Async Method Works!"];
[AsynClass method:parameter callbackFunction:^(BOOL callbackStatus, NSMutableArray* array) {
[expectation fulfil];
// whatever
}];
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
NSLog(@"expectation wait until handler finished ");
}];
// do it again
expectation = [self expectationWithDescription:@"Testing Async Method Works!"];
[CallBackClass method:parameter callbackFunction:^(BOOL callbackStatus, NSMutableArray* array) {
[expectation fulfil];
// whatever
}];
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
if (error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
NSLog(@"expectation wait until handler finished ");
}];
这似乎也适用于Swift 3.0。
let spyDelegate = SpyDelegate()
var asyncExpectation = expectation(description: "firstExpectation")
spyDelegate.asyncExpectation = asyncExpectation
let testee = MyClassToTest(delegate: spyDelegate)
testee.myFunction() //asyncExpectation.fulfill() happens here, implemented in SpyDelegate
waitForExpectations(timeout: 30.0) { (error: Error?) in
if let error = error {
XCTFail("error: \(error)")
}
}
asyncExpectation = expectation(description: "secoundExpectation")
spyDelegate.asyncExpectation = asyncExpectation
testee.delegate = spyDelegate
testee.myOtherFunction() //asyncExpectation.fulfill() happens here, implemented in SpyDelegate
waitForExpectations(timeout: 30.0) { (error: Error?) in
if let error = error {
XCTFail("error: \(error)")
}
}