2013-11-04 8 views
0

OCUnit을 사용하여 작업을 테스트하고 싶습니다.nsthread에서 무언가를 테스트하는 방법

- (BOOL)testThread 
{ 
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread) object:nil]; 
    [thread start]; 

    return YES; 
} 

- (void)thread 
{ 
    NSLog(@"thread**********************"); 
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread2) object:nil]; 
    [thread start]; 
} 

- (void)thread2 
{ 
    NSLog(@"thread2**********************"); 
} 

가 지금은 테스트 실행하려면 : 내 methodes의 하지만 하나 같이 내 테스트 케이스 에

- (void)testExample 
{ 
    testNSThread *_testNSThread = [[testNSThread alloc] init]; 
    STAssertTrue([_testNSThread testThread], @"test"); 
} 

을하지만 thread2는 어떻게해야합니까, 그래서 를 실행하지 복용량 ? 3Q! thread2가 완료 될 때까지

+0

을 나는이 그냥 코드 샘플과 질문과 아무 상관이 알고 있지만, thisNotation하지만 ThisNotation를 사용하지 않는 이름을 지정할 때 클래스 - 첫 번째 문자는 항상 대문자 여야합니다. 또한 지역 변수 이름의 밑줄은 적절하지 않으며 인스턴스 변수에 일반적으로 사용됩니다. – e1985

답변

1

당신은 testThread 대기를 만들기 위해 dispatch_semaphore를 사용할 수 있습니다

@interface MyTests() { 
    dispatch_semaphore_t semaphore; 
} 

@implementation MyTests 

- (void)setUp 
{ 
    [super setUp]; 
    semaphore = dispatch_semaphore_create(0); 
} 

- (BOOL)testThread 
{ 
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread) object:nil]; 
    [thread start]; 

    // Wait until the semaphore is signaled 
    while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) { 
     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]]; 
    } 

    return YES; 
} 

- (void)thread 
{ 
    NSLog(@"thread**********************"); 
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(thread2) object:nil]; 
    [thread start]; 
} 

- (void)thread2 
{ 
    NSLog(@"thread2**********************"); 

    // Signal the semaphore to release the wait lock 
    dispatch_semaphore_signal(semaphore); 
} 

@end 
+0

답을위한 thx, 그리고 난 runLoop 시간을 연장하여 sloved – LazyChen