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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -