使用 XCTest,如何将 {expectations -> wait} 的多个离散序列链接在一​​起?

Using XCTest, how can one chain together multiple discrete sequences of { expectations -> wait }?

XCTest waitForExpectationsWithTimeout:handler: 的文档指出

Only one -waitForExpectationsWithTimeout:handler: can be active at any given time, but multiple discrete sequences of { expectations -> wait } can be chained together.

但是,我不知道如何实现它,也找不到任何示例。我正在研究 class,它首先需要找到所有可用的串行端口,选择正确的端口,然后连接到连接到该端口的设备。因此,我正在处理至少两个期望,XCTestExpectation *expectationAllAvailablePorts 和 *expectationConnectedToDevice。我将如何链接这两个?

我执行以下操作并且有效。

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)")
    }
}

swift

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
} 

在扩展 XCTestCase 的 class 中,您可以像这样使用 wait(for:timeout:)

let expectation1 = self.expectation(description: "expectation 1")
let expectation2 = self.expectation(description: "expectation 2")
let expectation3 = self.expectation(description: "expectation 3")
let expectation4 = self.expectation(description: "expectation 4")

// ...
// Do some asyc stuff, call expectation.fulfill() on each of the above expectations.
// ...

wait(for:[expectation1,expectation2,expectation3,expectation4], timeout: 8)