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.
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.