0

次のいずれかの方法を使用してホーム ディレクトリを取得しようとすると、デフォルトの SenTestingkit を使用して iPhone シミュレーターでコードをテストするために sqlite データベースを使用するテスト ケースを実行しています。

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

また

NSString *home = NSHomeDirectory();

次のお風呂を指している間違ったパスを取得しました

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

sqlite データベースを作成できませんでした。テスト ケースで正しいパスを取得する方法を教えてください。

ありがとう

4

1 に答える 1

1

私は答えを見つけました:)

最初に、私が TestCase にいるか、SenTest クラスの NSClassFromString を使用してプロジェクトを実行しているかを検出し、次に各ケースに応じてパスを構築する必要がありました

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];
    }
于 2012-10-14T12:19:56.793 に答える