Используя XCTest, как можно объединить несколько дискретных последовательностей {ожидания → ждать}?
Документация для XCTest waitForExpectationsWithTimeout: обработчик:, указывает, что
Только один -waitForExpectationsWithTimeout: обработчик: может быть активным в любой момент времени, но несколько дискретных последовательностей {ожидания → ждать} могут быть соединены вместе.
Однако я не знаю, как это реализовать, и не могу найти никаких примеров. Я работаю над классом, который сначала должен найти все доступные последовательные порты, выбрать правильный порт и затем подключиться к устройству, подключенному к этому порту. Итак, я работаю, по крайней мере, с двумя ожиданиями, ожидания XCTest Expectation *AllAvailablePorts и * expectConnectedToDevice. Как бы связать эти два?
Ответы
Ответ 1
быстры
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
Я делаю следующее, и он работает.
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 ");
}];
Ответ 3
Присвоение моего ожидания слабой переменной сработало для меня. См. Объяснение здесь.
Ответ 4
Кажется, это работает и для меня в 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)")
}
}