Getting iPhone Documents Directory in TestCases

左心房为你撑大大i 提交于 2020-01-06 07:15:46

问题


I'm running test cases which uses sqlite database to test my code on iPhone simulator using default SenTestingkit, when i try to get the home directory using either of the following ways

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

or

NSString *home = NSHomeDirectory();

i got a wrong path that points to the following bath

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

and i couldn't create the sqlite database, any idea how to get the right path in the Test cases?

Thanks


回答1:


I found the answer :)

first I had to detect whether i'm in TestCase or running project using NSClassFromString for the SenTest Class then construct the path according to each case

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];
    }


来源:https://stackoverflow.com/questions/12872930/getting-iphone-documents-directory-in-testcases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!