我想測試一個使URL請求的方法。在NSURLConnectionDelegate
方法收到迴應時,我保存迴應。我真正想測試的邏輯是響應是否已保存。單元測試NSURLConnectionDelegate方法
如何測試NSURLConnectionDelegate
方法?
方法我想測試:
- (void) getProfilePictureURL :(NSString *)fbId
{
//to get picture URL
NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", fbId];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!connection)
{
NSLog(@"error while starting the connection");
}
}
// NSURLConnectionDelegate method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// Save response in user defaults
}
檢查響應是否保存在userdefault中?那就是你可以識別數據被保存的地方。 –
我知道,但只有在連接委託被調用並且代碼被執行後,我才能測試它。我如何檢查該代表是否被調用? – Geek
抱歉,但我不明白你的情況。你想在保存響應之前或保存響應之後檢查它? –