2009-07-30 44 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 
    ... 
相關問題