2009-07-30 86 views

回答

0

我能想到这样做的唯一原因是如果你想做一个实际的异步连接的功能测试。这将会非常脆弱,并且会减慢测试包的速度。

更好的办法是写你的NSURLConnection's delegate methods实现的单元测试,像connection:didFinishLoading:(成功案例)和connection:didFailWithError:(错误例)

我一般设置样品响应值,并验证我的委托处理它们正确。例如:

NSString *responseJSON = @"{\"remove\": [123,432], \"new\": [1,2]}"; 
    NSData *responseBytes = [responseJSON dataUsingEncoding:NSUTF8StringEncoding]; 

    // in requestHandler, data would have been set by calls to connection:didReceiveData: 
    [requestHandler setData:responseBytes]; 

    [requestHandler connectionDidFinishLoading:nil]; 

    // set expectations here for what you'll do with the response 
    ... 
相关问题