i am new in the ios line. I have a task to crete test case for this method.
+ (void)myMethod:(NSString *)paramA callback:(void(^)(NSString *result, BOOL success))callback;
// ... Some code that runs async (for example, something that fetches data from the internet)...
// Call the callback function in a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
callback(@"Success", YES);
});
}
As through rnd i get to kno something related to XCTestExpectation
used to create unit test cases....
Just call fulfill
in the compeletion block. For example:
Assume the FunctionObj
holds the function
@interface FunctionObj : NSObject
+ (void)myMethod:(NSString *)paramA callback:(void(^)(NSString *result, BOOL success))callback;
@end
@implementation FunctionObj
+ (void)myMethod:(NSString *)paramA callback:(void(^)(NSString *result, BOOL success))callback {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
callback(@"Success", YES);
});
}
@end
Then the test case should be
- (void)testAsyncFunction {
XCTestExpectation * expectation = [self expectationWithDescription:@"Test async method"];
[FunctionObj myMethod:@"1" callback:^(NSString *result, BOOL success) {
XCTAssert(success,"should be succeed");
XCTAssertNotNil(result,@"Should not be nil");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
//Do something when time out
}];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.