2012-01-19 2 views
0

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); 
} 
+2

http://stackoverflow.com/questions/1077737/ocunit-test-for-protocols-callbacks-delegate-in-objective-c –

+0

굉장합니다. 매력처럼 일했다. –

답변

0

첫 번째 : Stanislav의 링크가 우수합니다. 나 자신에게는 좀 더 유연한 (긴 지속 시간 동안) 무언가가 필요했지만 성공시 즉시 테스트를 통과해야했다.

-(BOOL)runLooperDooper:(NSTimeInterval)timeoutInSeconds 
     optionalTestName:(NSString *)testName 
{ 
    NSDate* giveUpDate = [NSDate dateWithTimeIntervalSinceNow:timeoutInSeconds]; 
    // loop until the operation completes and sets stopRunLoop = TRUE 
    // or until the timeout has expired 

    while (!stopRunLoop && [giveUpDate timeIntervalSinceNow] > 0) 
    { 
     // run the current run loop for 1.0 second(s) to give the operation code a chance to work 
     NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:1.0]; 
     [[NSRunLoop currentRunLoop] runUntilDate:stopDate]; 
    } 

    STAssertTrue(stopRunLoop, 
       @"%@ failed to finish before runloop expired after %f seconds", testName, timeoutInSeconds); 
    return stopRunLoop; 
} 

이 테스터 수준에 바르로 stopRunLoop를 선언 : (. 대형 파일 다운로드 등)

여기 내 유틸리티 함수입니다. setUp 함수에서 stopRunLoop을 FALSE로 재설정하고 [client processRequest:@"http://google.com" delegate:self];을 호출하기 전에이 함수를 호출해야합니다. 그런 다음 이벤트 처리기에서 stopRunLoop을 TRUE로 설정합니다.

testName을 전달하면이를 여러 테스트에 다시 사용하고 의미있는 오류 메시지를 얻을 수 있습니다.

편집 : 99 % 나는 내가 발견 할 수없는 다른 StackOverflow 포스트에서 위의 코드를 기반으로하고 있는지, 아니면 링크를했는지 확신합니다.

관련 문제