如何在块中使用Assert?

时间:2014-02-20 11:43:05

标签: ios asynchronous block assert gh-unit

我对如何在块中使用Assert有疑问?

例如:

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

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

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

}];
CFRunLoopRun();

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

但它会崩溃!

我该怎么做?

THX!

PS: - (void)testCase {     [自我准备];

__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];

}

1 个答案:

答案 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测试框架,它的语法很干净。