我正在尝试在 OCUnit 环境中运行我的应用程序。它使用 sqlite 数据库,我将其放在应用程序的文档文件夹中。
但是,当从测试运行时,数据库的路径设置为:
~/Library/Application Support/iPhone Simulator/6.0/Documents/Test.sqlite
虽然相同的代码给了我一个路径
~/Library/Application Support/iPhone Simulator/6.0/Applications/34536EFB-005A-44A1-AD99-07D6B9F6888B/Documents/Test.sqlite
当我在模拟器上运行它时。
如何在测试中使用 NSDocumentDirectory 并获得我需要的内容?
最佳答案
应用程序目标与测试目标不同,这就是您获得不同路径的原因。
使用 OCUnit 时,您可以获得测试 NSBundle
,如下所示:
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"myResource" ofType:@"someType"];
您必须将数据库添加到测试目标的资源中并相应地使用它。
或者您可以像这样构建路径(取自此 OS Answer ):
// Test case -> path inside "UnitTests" folder in the project directory
NSString *directory = [[NSFileManager defaultManager] currentDirectoryPath];
NSString *path = [directory stringByAppendingPathComponent:@"UnitTests/"];
path = [path stringByAppendingPathComponent:@"Test.sqlite"];
关于objective-c - 如何在单元测试中访问 NSDocumentDirectory (OCUnit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13034279/