Unit test method with data NSDataWithContentsOfURL

I am developing an iOS application and I am trying to do this with test development . I read a lot on this topic, and one of the things that I met in the book is that tests should be fast, repeatable, and reliable . As a result, when writing a test for network code, the test must "simulate" the network so that it can run regardless of network availability.

In my code, I have a method that extracts an image from the Internet with a URL, scales it and saves 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 testing this method so that the test passes when the code is correct and fails, if it is not, without taking into account possible network failures?

Currently, in my tests, I ignore the fact that the method needs to be connected, and just expect the result to be correct. However, I can 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 do not want my tests to fail just because there is no good network connection, because I believe that this is not their purpose. I appreciate any suggestions.

+1
ios unit-testing tdd nsdata
09 Oct '13 at 1:23
source share
1 answer

Your intention is to keep your tests fast, reliable and repeatable good. Removing network addiction is a great way to help achieve these goals.

Instead of being locked in the method as it is now, remember that the tool you are trying to use is called test- DRIVEN . Tests are not just a wrapper to make sure your code works as it is written. Its a way to learn how to improve your code to make it more reliable, reliable, reusable, etc.

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

I also notice that your method really works a lot. A name that describes what it really does can be loadImageFromURLAndScaleItAndSaveItToDisk :. Why not break it down into several smaller, more verifiable methods? Perhaps the one that downloads the image from a URL that scales the image and stores the image on the local file system. You can test each individual part, and then do another unit test that checks your original method, comparing its results with the results of calling smaller methods in order.

I think I would also say that a good test, reliable and repeatable tests is a good goal, but if you want to make the test "slower" or "not 1000% reliable", OK , a test that provides value for you is better than Do not write a test because you do not consider it a test good enough .

I know that most of them are common, but I hope this helps.

+1
Oct 09 '13 at 1:54 on
source share



All Articles