2013-03-20 38 views
0

我正在嘗試編寫測試用例來測試我的代表。例如,如何使用GHUnit測試代表回電

-(void)testUserLogin{ 
    loginRequest=[[RTUserLoginRequest alloc]initWithUserNmae:@"1235" password:@"1235"]; 
    [loginRequest setDelegate:self]; 
    [loginRequest invoke]; 


} 

-(void)userLoginRequest:(RTUserLoginRequest *)request didSucceed:(BOOL)succeed{ 

    GHTestLog(@"user login succeed"); 
} 

此代碼運行完美,但問題是當此回調激發它不顯示在像testUserLogin模擬器。什麼是測試這種情況的標準方法

+1

看到這個 http://stackoverflow.com/questions/1077737/ocunit-test-for-protocols-callbacks-delegate-in-objective -C – 2013-03-20 07:07:30

回答

0
-(void)testUserPendingRequest{ 
    userPendingRequest =[[RTCheckUserPendingRequest alloc]initWithUserNmae:@"123"]; 
    [userPendingRequest setDelegate:self]; 
    [userPendingRequest invoke]; 
    [self waitForCompletion:2]; 
    GHAssertTrue(_isDelegateInvoked, @"check user available invoked"); 

} 

-(void)checkUserPendingRequest:(RTCheckUserPendingRequest *)request isUserPending:(BOOL)pending{ 
    _isDelegateInvoked=YES; 
    if(pending) 
     GHTestLog(@"user Pending "); 
    else 
     GHTestLog(@"User Active"); 
} 

- (BOOL)waitForCompletion:(NSTimeInterval)timeoutSecs { 
    NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeoutSecs]; 

    do { 
     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate]; 
     if([timeoutDate timeIntervalSinceNow] < 0.0) 
      break; 
    } while (!_isDelegateInvoked); 

    return _isDelegateInvoked; 
}