iphone - unit test case for a asynchronous method ios -
i new in ios line. have task crete test case method.
+ (void)mymethod:(nsstring *)parama callback:(void(^)(nsstring *result, bool success))callback; // ... code runs async (for example, fetches data internet)... // call callback function in background thread dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ callback(@"success", yes); }); }
as through rnd kno related xctestexpectation
used create unit test cases....
just call fulfill
in compeletion block. example:
assume functionobj
holds 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 test case should be
- (void)testasyncfunction { xctestexpectation * expectation = [self expectationwithdescription:@"test async method"]; [functionobj mymethod:@"1" callback:^(nsstring *result, bool success) { xctassert(success,"should succeed"); xctassertnotnil(result,@"should not nil"); [expectation fulfill]; }]; [self waitforexpectationswithtimeout:10 handler:^(nserror *error) { //do when time out }]; }
Comments
Post a Comment