XCTestExpectation: how to avoid calling the fulfill method after the wait context has ended?
Yes, there is a much simpler way to avoid this API violation issue: just declare your expectation variable as __weak
. Although not clearly documented, the expectation will be released when the timeout expires. So if the task takes longer than the timeout, the expectation variable will be nil when the task completion handler is called. Thus the fulfill
method will be called on nil, doing nothing.
- (void) test1TaskLongerThanTimeout
{
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Test 1: task longer than timeout"];
[self startAsynchronousTaskWithDuration:4 completionHandler:^(id result, NSError *error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:2 handler:nil];
}
Asynchronous Performance Tests with XCTest
With some help from Apple, I have a solution. Silly oversight on my part as this is very easy to solve. To get to work, all you need to do is put the creating of the expectation object (clsQueryReturnedExpectation) inside the measureMetrics block so it is created afresh each time the performance test is run.
PFCLSClient *theClient = [[PFCLSClient alloc] init];
[self measureMetrics:@[XCTPerformanceMetric_WallClockTime] automaticallyStartMeasuring:YES forBlock: ^{
XCTestExpectation *clsQueryReturnedExpectation = [self expectationWithDescription:@"clsQuery returned"];
[theClient getStructureOfType:clsImageTypeSVG ForID:idString success: ^(NSDictionary *structureInfo) {
[clsQueryReturnedExpectation fulfill];
} failure: ^(NSError *error) {
XCTFail();
[clsQueryReturnedExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:5 handler: ^(NSError *error) {
[self stopMeasuring];
}];
}];
Related Topics
Swift-Setting a Physics Body Velocity by Angle
Adding Animation to Tabviews in Swiftui When Switching Between Tabs
Compile Time Key Path Checking in Swift
Can My Class Override Protocol Property Type in Swift
Swift: Print Name of a Function Stored in a Variable
Swiftui: How to Find the Height of an Image and Use It to Set the Size of a Frame
Xcode - Build Setting "Excluded_Source_File_Names" Not Working
Check If a Func Exists in Swift
How to Make a Segue to Second Item of Tab Bar
Why Use an Extra Let Statement Here
How to Insert a Sublayer in Swift
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Swift Language Statically or Dynamically Dispatched
Swiftui View Does Not Updated When Observedobject Changed