簡體   English   中英

如何在塊中使用斷言?

[英]How to use the Assert in a block?

我對如何在一個塊中使用Assert有疑問?

例如:

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

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

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

}];
CFRunLoopRun();

它必須發送一個異步請求。 完成后,我需要斷言它是否成功。

但這會崩潰!

我該怎么辦?

謝謝!

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

}

看一下文檔async部分 (主要是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測試框架,它的語法很不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM