ios – Xcode单元测试和将数据写入磁盘

前端之家收集整理的这篇文章主要介绍了ios – Xcode单元测试和将数据写入磁盘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试编写逻辑测试来调试将图像数据保存到磁盘的方法.我使用以下行将图像数据写入文档目录:

docspath = [[NSString alloc] initWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,TRUE) objectAtIndex:0]];
fullpath = [[NSString alloc] initWithFormat:@"%@/%@",docspath,_name]; 
BOOL written = [fileData writeToFile:fullpath options:NSDataWritingAtomic error:&dataerror];

除外,不写入数据,错误描述不返回此类文件或目录.我在iOS 5.1模拟器中的测试目标下运行此代码.我是否需要创建中间目录才能到达文档目录,或者在测试目标下写入磁盘还有其他问题吗?

错误返回:

2012-06-29 16:13:31.819 otest[4597:7b03] /Users/myaccount/xcode_projects/myproject/myproject/myfolder/MMFileManager.m - 285: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x1e1e3a0 {NSUserStringVariant=Folder,NSFilePath=/Users/myaccount/Library/Application Support/iPhone Simulator/5.1/Documents/mmlogo.png,NSUnderlyingError=0x1e1b430 "The operation couldn’t be completed. No such file or directory"}

解决方法

单元测试不在您的应用程序中,因此,您无法在单元测试中访问Documents目录.

参考NSHomeDirectory in iPhone unit test

猜你在找的Xcode相关文章