在测试用例中获取iPhone文档目录

时间:2012-10-13 12:03:42

标签: sqlite ios5 xcode4 testcase sentestingkit

我正在运行使用sqlite数据库的测试用例,使用默认的SenTestingkit在iPhone模拟器上测试我的代码, 当我尝试使用以下任一方式获取主目录时

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];

NSString *home = NSHomeDirectory();

我得到了一条错误的路径,指向下面的浴缸

/Users/{user}/Library/Application Support/iPhone Simulator/5.1

我无法创建sqlite数据库, 任何想法如何在测试用例中获得正确的路径?

由于

1 个答案:

答案 0 :(得分:1)

我找到了答案:)

首先,我必须检测我是在TestCase中还是使用NSClassFromString为SenTest类运行项目,然后根据每种情况构建路径

if (NSClassFromString(@"SenTest") == nil) {
        // Running project -> path inside the Document folder
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docsPath = [paths objectAtIndex:0];
        path = [docsPath stringByAppendingPathComponent:KDatabaseName];
    } else {
        // Test case -> path inside "UnitTests" folder in the project directory
        NSString *directory = [[NSFileManager defaultManager] currentDirectoryPath];
        path = [directory stringByAppendingPathComponent:@"UnitTests/"];
        path = [path stringByAppendingPathComponent:KDatabaseName];
    }