コールバックでアサーションを使用すると、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 {
}