方法代码如下:
- (void)downloadSomething:(NSString *)url {
Downloader *downloader = [[Downloader alloc] initWithUrl:url];
NSData *data = [downloader download];
FileCache *cache = [[FileCache alloc] initWithFile:@"download.cache"];
cache.data = data;
[cache save];
}
我想我应该模拟 Downloader 和 FileCache 来验证它是否运行良好。我曾考虑过这样的更改签名:downloadSomething:(NSString *)url downloader:(Downloader *)downloader cache:(FileCache *)cache
,但在调用此方法之前似乎需要做很多工作,这不是我想要的。
我正在使用 ocmockito。
此外,是否有使编写代码更可测试的指南?
编辑:2017-01-16 14:54:23
编写两种方法是否是个好主意,例如:
- (void)updateCacheWithUrl:(NSString *)url
downloader:(Downloader *)downloader
fileCache:(FileCache *)fileCache; // for testing
- (void)updateCacheWithUrl:(NSString *)url; // call above method with (url, nil, nil);