如何对NSURLConnection代表进行单元测试? [英] how to unit test a NSURLConnection Delegate?

查看:93
本文介绍了如何对NSURLConnection代表进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对NSURLConnection委托进行单元测试?
我创建了一个ConnectionDelegate类,它符合不同的协议,可以将数据从Web提供给不同的ViewControllers。在我走得太远之前,我想开始编写单元测试。
但我不知道如何在没有互联网连接的情况下测试它们。我还想对待异步回调我应该做些什么。

How can I unit test my NSURLConnection delegate? I made a ConnectionDelegate class which conforms to different protocols to serve data from the web to different ViewControllers. Before I get too far I want to start writing my unit tests. But I don't know how to test them as a unit without the internet connection. I would like also what I should do to treat the asynchronous callbacks.

推荐答案

这类似于Jon的回复,不适合然而,它成了评论。第一步是确保您没有创建真正的连接。实现此目的的最简单方法是将连接的创建拉入工厂方法,然后在测试中替换工厂方法。有了OCMock的部分模拟支持,这可能看起来像这样。

This is similar to Jon's response, couldn't fit it into a comment, though. The first step is to make sure you are not creating a real connection. The easiest way to achieve this is to pull the creation of the connection into a factory method and then substitute the factory method in your test. With OCMock's partial mock support this could look like this.

在真正的班级中:

- (NSURLConnection *)newAsynchronousRequest:(NSURLRequest *)request
{
    return [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

在您的测试中:

id objectUnderTest = /* create your object */
id partialMock = [OCMockObject partialMockForObject:objectUnderTest];
NSURLConnection *dummyUrlConnection = [[NSURLConnection alloc] 
    initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:foo"]] 
    delegate:nil startImmediately:NO];
[[[partialMock stub] andReturn:dummyUrlConnection] newAsynchronousRequest:[OCMArg any]];

现在,当您的测试对象尝试创建URL连接时,它实际上获得了创建的虚拟连接考试。虚拟连接不必有效,因为我们没有启动它并且它永远不会被使用。如果您的代码确实使用了连接,则可以返回另一个模拟,模拟NSURLConnection。

Now, when your object under test tries to create the URL connection it actually gets the dummy connection created in the test. The dummy connection doesn't have to be valid, because we're not starting it and it never gets used. If your code does use the connection you could return another mock, one that mocks NSURLConnection.

第二步是调用对象上触发创建的方法NSURLConnection:

The second step is to invoke the method on your object that triggers the creation of the NSURLConnection:

[objectUnderTest doRequest];

因为测试中的对象没有使用真正的连接,我们现在可以从测试中调用委托方法。对于NSURLResponse,我们使用另一个模拟,响应数据是从测试中其他地方定义的字符串创建的:

Because the object under test is not using the real connection we can now call the delegate methods from the test. For the NSURLResponse we're using another mock, the response data is created from a string that's defined somewhere else in the test:

int statusCode = 200;
id responseMock = [OCMockObject mockForClass:[NSHTTPURLResponse class]];
[[[responseMock stub] andReturnValue:OCMOCK_VALUE(statusCode)] statusCode];
[objectUnderTest connection:dummyUrlConnection didReceiveResponse:responseMock];

NSData *responseData = [RESPONSE_TEXT dataUsingEncoding:NSASCIIStringEncoding];
[objectUnderTest connection:dummyUrlConnection didReceiveData:responseData];

[objectUnderTest connectionDidFinishLoading:dummyUrlConnection];

就是这样。你已经有效地伪造了被测对象与连接的所有交互,现在你可以检查它是否处于它应该处于的状态。

That's it. You've effectively faked all the interactions the object under test has with the connection, and now you can check whether it is in the state it should be in.

如果你想要查看一些真实代码,请查看使用NSURLConnections的CCMenu项目中的类的测试。这有点令人困惑,因为测试的类也被称为连接。

If you want to see some "real" code, have a look at the tests for a class from the CCMenu project that uses NSURLConnections. This is a little bit confusing because the class that's tested is named connection, too.

http://ccmenu.svn.sourceforge.net/viewvc/ccmenu/trunk/CCMenuTests/Classes/CCMConnectionTest .m?revision = 129& view = markup

这篇关于如何对NSURLConnection代表进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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