如何使用GHUnit测试委托回调

时间:2013-03-20 06:16:45

标签: iphone ios xcode unit-testing

我正在尝试为测试我的代表编写测试用例。例如,

-(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 个答案:

答案 0 :(得分: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;
}