Search code examples
sqliteios5xcode4testcasesentestingkit

Getting iPhone Documents Directory in TestCases


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


Solution

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