在回调中使用任何断言都会导致 GH-Unit 应用程序崩溃。断言在其他地方工作得很好。
这里有一个类似的问题:为什么 GHUnit 中的异步测试中的错误断言会使应用程序崩溃,而不是仅仅使测试失败?
但我不明白如何在我的情况下使用这个解决方案。
- (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];
}
我可以通过覆盖此方法来阻止应用程序崩溃 - 我什至可以记录异常,但测试不会在前端显示为失败。理想情况下,我希望它显示在前端,以便非技术人员可以运行测试并查看一切正常。
- (void)failWithException:(NSException *)exception {
}