2014-02-20 50 views
0

我有关于如何在块中使用Assert的问题?如何在块中使用断言?

例如:

[someObject postRequestWithBlock:^(BOOL succeeded, NSError *error) { 

    CFRunLoopRef runLoopRef = CFRunLoopGetCurrent(); 
    CFRunLoopStop(runLoopRef); 

    GHAssertTrue(succeeded&&!error, @"faile"); 

}]; 
CFRunLoopRun(); 

它必须发送的异步请求。 完成后,我需要声明它是否成功。

但它会崩溃!

我该怎么办?

thx!

PS: - (void)testCase { [self prepare];

__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; 

__block typeof (&*self) bself = self; 

[request setCompletionBlock:^{ 

    [bself notify:kGHUnitWaitStatusSuccess forSelector:@selector(testCase)]; 
    GHAssertTrue(YES, @"faile"); 
}]; 

[request setFailedBlock:^{ 

    [bself notify:kGHUnitWaitStatusFailure forSelector:@selector(testCase)]; 
    GHAssertTrue(NO, @"faile"); 
}]; 

[request startAsynchronous]; 

[self waitForStatus:kGHUnitWaitStatusCancelled timeout:15.0]; 

}

+0

你不想断言完成是错误的,错误是真的吗?你有他们的另一种方式。 –

回答

0

看一看在async section of the docs(主要是ExampleAsyncTest.m:下位)。喜欢的东西:

-(void)testCase 
{ 
    [someObject postRequestWithBlock:^(BOOL succeeded, NSError *error) { 

     [self notify:succeeded ? kGHUnitWaitStatusSuccess : kGHUnitWaitStatusFailure forSelector:@selector(testCase)]; 

     GHAssertTrue(succeeded&&!error, @"faile"); 

    }]; 

    [self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];  
} 

我会建议Expecta测试框架也是如此,它有一个干净的语法。

+0

非常感谢您的帮助! 但我仍然在这里,有一个问题。 如果在这个块中断言,它会崩溃。 我的阻挡是问题吗? – user3332433