测试NSURLConnection与HTTP响应错误状态的使用 [英] Testing use of NSURLConnection with HTTP response error statuses

查看:131
本文介绍了测试NSURLConnection与HTTP响应错误状态的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要从Web服务器获取一些数据的iPhone应用程序。我正在使用 NSURLConnection 来执行HTTP请求,这很有效,但在响应有HTTP错误代码的情况下我无法对代码进行单元测试(像404或500)。

I'm writing an iPhone application that needs to get some data from a web server. I'm using NSURLConnection to do the HTTP request, which works well, but I'm having trouble unit testing my code in the case where the response has an HTTP error code (like 404 or 500).

我正在使用 GTM OCMock 嘲笑。

当服务器返回错误时,连接不会在委托上调用 connection:didFailWithError:,但是调用 connection:didReceiveResponse: connection:didReceiveData:,以及 connectionDidFinishLoading:相反。我正在检查连接中的响应的状态代码:didReceiveResponse:并在连接时调用取消状态代码看起来像是一个错误,以防止 connectionDidFinishLoading:被调用,其中将报告成功的响应。

When the server returns an error, the connection does not call connection:didFailWithError: on the delegate, but calls connection:didReceiveResponse:, connection:didReceiveData:, and connectionDidFinishLoading: instead. I'm currently checking the status code on the response in connection:didReceiveResponse: and calling cancel on the connection when the status code looks like an error to prevent connectionDidFinishLoading: from being called, where a successful response would be reported.

提供静态存根 NSURLConnection 很简单,但我希望我的测试在调用其中一个模拟连接的方法时改变它的行为。具体来说,我希望测试能够告诉代码何时在模拟连接上调用 cancel ,因此测试可以停止调用 connection:didReceiveData : connectionDidFinishLoading:代表。

Providing a static stubbed NSURLConnection is simple, but I want my test to change it's behaviour when one of the mock connection's methods is called. Specifically, I want the test to be able to tell when the code has called cancel on the mock connection, so the test can stop calling connection:didReceiveData: and connectionDidFinishLoading: on the delegate.

有没有办法进行测试告诉你是否在模拟对象上调用了 cancel ?有没有更好的方法来测试使用 NSURLConnection 的代码?有没有更好的方法来处理HTTP错误状态?

Is there a way for tests to tell if cancel has been called on the mock object? Is there a better way to test code that uses NSURLConnection? Is there a better way to handle HTTP error statuses?

推荐答案


有没有更好的方法来处理HTTP错误状态?

Is there a better way to handle HTTP error statuses?

我认为你走在正确的轨道上。我使用类似于以下代码的内容,我发现此处

I think you are on the right track. I use something similar to the following code, which I found here:

if ([response respondsToSelector:@selector(statusCode)])
{
    int statusCode = [((NSHTTPURLResponse *)response) statusCode];
    if (statusCode >= 400)
    {
        [connection cancel];  // stop connecting; no more delegate messages
        NSDictionary *errorInfo
          = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
            NSLocalizedString(@"Server returned status code %d",@""),
            statusCode]
                                        forKey:NSLocalizedDescriptionKey];
        NSError *statusError
          = [NSError errorWithDomain:NSHTTPPropertyStatusCodeKey
                                code:statusCode
                            userInfo:errorInfo];
        [self connection:connection didFailWithError:statusError];
    }
}

取消连接,并调用 connection:didFailWithError:,以使http错误代码的行为与任何其他连接错误完全相同。

This cancels the connection, and calls connection:didFailWithError: in order to make http error codes behave exactly the same as any other connection error.

这篇关于测试NSURLConnection与HTTP响应错误状态的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆