wait(for:)
Waits on a group of expectations.
Declaration
func wait(for expectations: [XCTestExpectation]) -> XCTWaiter.ResultParameters
- expectations:
An array of expectations the test must satisfy.
Return Value
A value describing the outcome of waiting for expectations.
Discussion
The following example demonstrates how to wait on exceptions:
- (void)testSprockets {
// The test allows 60 seconds total for execution.
self.executionTimeAllowance = 60;
MySprocket *sprocket = [[MySprocket alloc] init];
XCTestExpectation *sprocketLoaded = [self expectationWithDescription:@"sprocket loaded"];
[sprocket loadUsingBlock:^(NSUInteger toothCount) {
if (toothCount == 6) {
[sprocketLoaded fulfill];
}
}];
[self waitForExpectations:@[sprocketLoaded]];
id orbitWobbled = [self keyValueObservingExpectationForObject:sprocket
keyPath:@"orbitAngle.doubleValue"
expectedValue:@90.0];
dispatch_async(myQueue, ^ {
[sprocket wobbleOrbit];
});
[self waitForExpectations:@[orbitWobbled]];
}Expectations can only appear in the array once.
The call may return before the timeout if the test fulfills all the expectations you provide.
In Objective-C code, you might use an expectation to wait on a call to an interface that uses a completion handler to return a result. From Swift code, consider calling withCheckedContinuation(function:_:) to use Concurrency instead of an expectation to wait on the result of a completion handler.