我在SenTestCase類中有以下Objective C測試代碼。我沒有得到任何錯誤,但httpReceiveDataFinished方法永遠不會被調用。這是因爲在委託有機會處理http方法之前測試結束了嗎?如何使測試方法等待代理完成處理?
如果是這種情況,我該如何分離線程(或類似的東西)以使測試等待幾秒鐘?
非常感謝任何幫助。我已經編寫了Java多年,但Objective-C只有幾天。
- (void)testExample
{
HttpClient *client = [[HttpClient alloc] init];
client.method = METHOD_GET;
client.followRedirects = YES;
[client processRequest:@"http://google.com" delegate:self];
NSLog(@"Test Over");
}
-(void) httpReceiveError:(NSError*)error {
NSLog(@"***\n%@\n***",[error description]);
}
- (void) httpReceiveDataChunk:(NSData *)data {
[self.httpResponseData appendData:data];
}
-(void) httpReceiveDataFinished {
NSString *result = [[NSString alloc]
initWithData:self.httpResponseData
encoding:NSUTF8StringEncoding];
NSLog(@"***\nRESULT: %@ \n***",result);
}
http://stackoverflow.com/questions/1077737/ocunit-test-for-protocols-callbacks-delegate-in-objective-c –
真棒。像魅力一樣工作。 –