首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在回调块中运行断言而不是在前端显示错误时,GHUnit崩溃

在回调块中运行断言而不是在前端显示错误时,GHUnit崩溃
EN

Stack Overflow用户
提问于 2012-02-14 10:05:48
回答 2查看 518关注 0票数 2

在回调中使用任何断言都会导致GH-Unit应用程序崩溃。断言在其他地方工作得很好。

这里有一个类似的问题:Why does a false assertion in async test in GHUnit crash the app instead of just failing the test?

但我不明白如何在我的案例中使用这种解决方案。

代码语言:javascript
复制
- (void)testLoadMyProfile {

    void(^successCallback)(NSString*);
    successCallback = ^(NSString* response) {

        NSRange textRange;
        textRange =[[response lowercaseString] rangeOfString:[@"syntactically incorrect" lowercaseString]];

        if(textRange.location != NSNotFound) {
            GHFail(@"the request was syntactically incorrect");
        }

        NSDictionary *d;        
        @try {
            d = [response JSONValue];    
        } @catch (NSException *exception) {
            GHAssertNotNil(d, @"The response was not a valid JSONValue");
        }

        GHAssertNotNil([d objectForKey:@"memberId"], @"memberId wasn't in response");
        GHAssertNotNil([d objectForKey:@"profile"], @"profile wasn't in response");
        GHAssertNotNil([d objectForKey:@"name"], @"profile wasn't in response");
        GHAssertNotNil([d objectForKey:@"surnamez"], @"profile wasn't in response");
    };

    void(^errorCallback)(NSString*);
    errorCallback = ^(NSString* response) {
        GHFail(@"the error callback was called");
    };    

    // this is using ASIHTTPRequest to retrieve data
    [[RestAPIConnector sharedInstance] loadMyProfile:successCallback :errorCallback];
}

我可以通过覆盖此方法来阻止应用程序崩溃-我甚至可以记录异常,但测试在前端不会显示为失败。理想情况下,我希望它显示在前端,这样非技术人员就可以运行测试,并看到一切都在工作。

代码语言:javascript
复制
- (void)failWithException:(NSException *)exception {

}
EN

回答 2

Stack Overflow用户

发布于 2012-04-14 21:24:43

你应该改变状态来停止块运行循环,检查我的评论:(对不起,我的英语不好)

首先,您必须继承GHAsyncTestCase的子类

代码语言:javascript
复制
-(void)testGetRequest{
    [self prepare]; //prepare for hook the run loop

    PLRequest *req=[PLRequest requestWithURL:[NSURL URLWithString:@"http://m.baidu.com"]];
    req.finishCallback=^(PLRequest *req){
        NSData *d=req.respondData;

        NSString *s=[NSString stringWithUTF8String:[d bytes]];

        GHTestLog(@"Finish:%@", s);
        [self notify:kGHUnitWaitStatusSuccess]; //here to return
    };

    [req start];

    [self waitForStatus:kGHUnitWaitStatusSuccess timeout:15]; //wait for your block change Status then if timeout you will get a 'crash'

}
票数 1
EN

Stack Overflow用户

发布于 2013-01-24 09:45:30

答案(您所指的) here也应该适用于您。

因此,您应该像Travis之前提到的那样实现GHAsyncTestCase。有了异步基类,您就可以使用waitForStatus:timeout:和相应的notify:forSelector:方法。所有断言都需要在waitForStatus:timeout:之后完成。此方法暂停运行循环,并等待回调完成。

如果您需要有关异步测试的更多信息,请查看samples of GHUnit

所以在你的例子中,我会尝试这样:

代码语言:javascript
复制
- (void)testLoadMyProfile {

//Local variable for later assertion. __block is necessary to use the variable in the block.  
__block NSDictionary *d = nil;  

//Preparing.  
[self prepare];

void(^successCallback)(NSString*);
successCallback = ^(NSString* response) {

    NSRange textRange;
    textRange =[[response lowercaseString] rangeOfString:[@"syntactically incorrect" lowercaseString]];

    if(textRange.location != NSNotFound) {
        GHFail(@"the request was syntactically incorrect");
    }

    // Declared before.
    // NSDictionary *d;

    @try {

        d = [response JSONValue];    

    } @catch (NSException *exception) {

        // We'll check that later.
        // GHAssertNotNil(d, @"The response was not a valid JSONValue");
    }

    // Later.
    //GHAssertNotNil([d objectForKey:@"memberId"], @"memberId wasn't in response");
    //GHAssertNotNil([d objectForKey:@"profile"], @"profile wasn't in response");
    //GHAssertNotNil([d objectForKey:@"name"], @"profile wasn't in response");
    //GHAssertNotNil([d objectForKey:@"surnamez"], @"profile wasn't in response");

    // we notify ourself that we are done. selector should point to this method!  
   [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testLoadMyProfile)];  

};

void(^errorCallback)(NSString*);
errorCallback = ^(NSString* response) {
    GHFail(@"the error callback was called");

    // in this case we do notify ourself that the request failed.  
   [self notify:kGHUnitWaitStatusFailure forSelector:@selector(testLoadMyProfile)];

};    

[[RestAPIConnector sharedInstance] loadMyProfile:successCallback :errorCallback];

// This line pauses the runloop for the length of the timeout
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];

// And finally after this line you can do your assertions. As the code pauses until you notify it or the timeout fires.
GHAssertNotNil(d, @"The response was not a valid JSONValue");
GHAssertNotNil([d objectForKey:@"memberId"], @"memberId wasn't in response");
GHAssertNotNil([d objectForKey:@"profile"], @"profile wasn't in response");
GHAssertNotNil([d objectForKey:@"name"], @"profile wasn't in response");
GHAssertNotNil([d objectForKey:@"surnamez"], @"profile wasn't in response");

}

因此,异步测试的所有断言都需要在waitForStatus:timeout:之后完成。别忘了notify:forSelector

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9270758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档