问题 使用XCTest,如何将{期望 - >等待}的多个离散序列链接在一​​起?


XCTest waitForExpectationsWithTimeout:handler:的文档说明了这一点

只有一个-waitForExpectationsWithTimeout:handler:可以在任何给定时间处于活动状态,但是{expected - > wait}的多个离散序列可以链接在一起。

但是,我不知道如何实现它,也找不到任何例子。我正在开发一个类,首先需要找到所有可用的串口,选择正确的端口,然后连接到连接到该端口的设备。所以,我正在处理至少两个期望,XCTestExpectation * expectationAllAvailablePorts和* expectationConnectedToDevice。我怎么把这两个连在一起?


12771
2018-03-19 16:44


起源



答案:


迅速

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
} 

2
2017-11-30 17:23



该技术终于赶上了文档。我一直在Objective-C中使用它,它确实有效。 - Elise van Looij


答案:


迅速

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
} 

2
2017-11-30 17:23



该技术终于赶上了文档。我一直在Objective-C中使用它,它确实有效。 - Elise van Looij


我做了以下工作。

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

7
2018-05-03 05:17





将我的期望分配给弱变量对我有用。


3
2017-12-07 12:26



这样做了!!谢谢你提到弱变量! - Nitin Alabur
警告: 上面的“此处”链接可能不再有效。它现在提供了一个“需要”您安装“Flash Player Pro”的页面,而不是关于弱变量的页面。单击网站上的[x]按钮以“关闭”警告,将粗略的.dmg文件下载到我的Mac。我没打开它。 WMMV。 - Bill Feth


这似乎也适用于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)")
    }
}

0
2018-04-05 14:43