使用 NSData dataWithContentsOfURL 的单元测试方法 [英] Unit test method with NSData dataWithContentsOfURL

查看:94
本文介绍了使用 NSData dataWithContentsOfURL 的单元测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 iOS 应用程序,我正在尝试使用测试驱动开发来完成它.我已经阅读了很多关于这个主题的文章,我在 是测试应该快速、可重复和可靠.因此,在编写网络代码测试时,测试应该模拟"网络,以便无论网络可用性如何都可以执行.

I am developing an iOS app and I am trying to do it with test-driven development. I've been reading a lot on the subject and one of the things I came across in a book is that tests are supposed to be fast, repeatable and reliable. As a consequence, when writing a test for networking code, the test should "simulate" the network, so that it can be executed regardless of network availability.

在我的代码中,我有一种方法可以使用 URL 从 Internet 检索图像,使用来自 NSData 的 dataWithContentsOfURL 对其进行缩放并将其存储在文件系统中:

In my code I have a method that retrieves an image from the internet with a URL, scales it and stores it in the file system using dataWithContentsOfURL from NSData:

+ (void) getImage:(NSString *)imageUrl {

    UIImage *image = [UIImage imageWithData:
        [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];

    // Scale image
    // Save image in storage 
}

我的问题是:测试此方法的好方法是什么,以便在代码正确时测试通过而在不正确时失败,而不考虑可能的网络故障?

My question is: what would be a good approach to test this method, so that the test passes when the code is correct and fails when it isn't, without taking into account possible network failures?

目前在我的测试中,我忽略了该方法需要连接的事实,只希望结果是正确的.然而,我已经看到当我的测试套件增长时这会成为一个问题,因为它们已经需要相当长的时间来运行.更重要的是,我不希望我的测试仅仅因为没有良好的网络连接而失败,因为我相信这不是他们的目的.我很感激任何建议.

Currently in my tests I am ignoring the fact that the method needs a connection, and just expect the outcome to be correct. However, I already see how this can become a problem when my test suit grows, since they already take a considerable amount of time to run. More importantly, I don't want my tests to fail just because there isn't a good network connection, as I believe that is not their purpose. I appreciate any suggestions.

推荐答案

保持测试快速、可靠和可重复的意图是好的.消除对网络的依赖是帮助实现这些目标的好方法.

Your intention to keep your tests fast, reliable and repeatable are good ones. Removing the dependency on the network is a great way to help achieve those goals.

不要像现在那样被锁定在方法中,请记住,您尝试使用的工具称为测试-驱动开发.测试不仅仅是确保您的代码按编写方式工作的包装器.它是一种发现改进代码的方法,使其更具可测试性、可靠性、可重用性等.

Instead of being locked into the method as it is now, remember that the tool you are trying to use is called test-DRIVEN development. Tests aren't just a wrapper to make sure your code works as written. Its a way to discover ways that your code can be improved to make it more testable, reliable, reusable, etc.

为了消除网络依赖性,您可以考虑传递一个 URL(您的参数名称已经是 imageURL)而不是一个字符串.通过这种方式,您可以创建指向本地文件系统的 URL,而不是指向网络上的某个位置.

To eliminate the network dependency, you could consider passing a URL (your parameter's name is already imageURL) instead of a string. This way you can create URL to the local file system instead of to a location on the network.

我也注意到你的方法确实做了很多工作.描述它真正做什么的名称可能是 loadImageFromURLAndScaleItAndSaveItToDisk:.为什么不把它分解成几个更小、更可测试的方法呢?也许一个从 URL 加载图像,一个缩放图像,一个将图像保存到本地文件系统.您可以测试每个单独的部分,然后进行另一个单元测试,通过将原始方法的结果与按顺序调用较小方法的结果进行比较来测试您的原始方法.

I also notice that your method is really doing a lot of work. A name that describes what its really doing might be loadImageFromURLAndScaleItAndSaveItToDisk:. Why not break it up into several smaller, more testable methods? Maybe one that loads an image from a URL, one that scales an image and one that saves an image to the local file system. You could test each individual portion and then make another unit test that tests your original method by comparing its results against the results of calling the smaller methods in order.

我想我还会说保持测试快速、可靠和可重复是一个很好的目标,但是如果您想要进行较慢"或不是 1000% 可靠"的测试,好的.为您提供价值的测试比不编写测试要好,因为您认为它不够好测试.

I guess I would also say that keeping tests fast, reliable and repeatable is a good goal, but if you want make a test that is 'slower' or 'not 1000% reliable' that OK. A test that provides value to you is better than not writing a test because you don't think its a good enough test.

我知道大部分内容很笼统,但我希望它有所帮助.

I know most of that is general, but I hope it helps.

这篇关于使用 NSData dataWithContentsOfURL 的单元测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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