【问题标题】:How to access NSDocumentDirectory in unit tests (OCUnit)如何在单元测试(OCUnit)中访问 NSDocumentDirectory
【发布时间】:2012-10-23 15:44:00
【问题描述】:

我正在尝试在 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 并获得我需要的东西?

【问题讨论】:

    标签: objective-c ios cocoa-touch cocoa ocunit


    【解决方案1】:

    应用程序目标与测试目标不同,这就是你得到不同路径的原因。

    使用 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"];
    

    【讨论】:

      猜你喜欢
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多