Вопрос

I had questions about how to use the Assert in a block?

For example:

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

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

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

}];
CFRunLoopRun();

It will have to send an asynchronous request . After it is completed, I need to Assert whether it is successful.

But it's going to crash!

How i should do?

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

}

Это было полезно?

Решение

Have a look at the async section of the docs (mainly the bit under ExampleAsyncTest.m:). Something like:

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

I would recommend the Expecta testing framework too, its got a nice clean syntax.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top