【问题标题】:OCUnit - iOS 5 - How do I test files in my app?OCUnit - iOS 5 - 如何在我的应用程序中测试文件?
【发布时间】:2012-03-29 19:01:31
【问题描述】:

我正在尝试构建简单的测试来查看我需要的文件(图像、视频、音频文件)是否在我的应用程序中。每当我尝试做以下事情时:

if (![[NSFileManager defaultManager] fileExistsAtPath:@"Menu Loop.mp3"])
{ 
    STFail(@"File does not exist");
}
else
{
    NSLog(@"Passed");
}

它说文件不存在。我可以看到文件在那里,我知道它失败了,因为文件在 app 文件夹中,而不是在 test 文件夹中。有没有办法离开测试文件夹来测试文件是否存在而不将所有文件添加到测试文件夹?我曾尝试执行诸如 fileExistsAtPath:@"../appfolder/Menu Loop.mp3" 之类的操作来尝试导航出测试文件夹,但不幸的是,它也无法以这种方式工作。

提前致谢!

【问题讨论】:

    标签: iphone ios testing ocunit


    【解决方案1】:

    您不能通过 iOS SDK 直接访问这样的文件夹。您必须使用返回正确路径的系统函数来保存和读取文件:

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
    NSString *pathToMenuLoopFile = [rootPath stringByAppendingPathComponent:@"Menu Loop.mp3"];
    

    如果您希望加载包含在 Xcode 项目中的文件,请查看此帖子:Loading data files in iPhone project

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多