0

テストスイートを起動して実行するのに少し苦労しています。基本的に、次のように新しいターゲットをアプリケーションに追加しました: Unit Testing Applications

DA クラスと sqlite-database に対してテストしたいので、次の抜粋を使用してデータベースをコピーします。

    // First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_Path];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DATABASE_Path];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

しかし、NSBundle は期待どおりにデータベースへのパスを返さないようです。

これに対する簡単な修正はありますか?この数時間で修正できなかったので、次は皆さんです。

どんな助けや提案も大歓迎です!

どうもありがとう!

トム

4

1 に答える 1

1

まったく同じ問題に遭遇し、生のsqliteアクセスを非常によく似た方法で実行するコードを継承しました。

ここには 2 つの問題があり、SO の別の場所で回答がありました。

まず、NSBundle mainBundle のテスト中に、期待どおりの結果が返されません。単体テスト中に NSBundle mainBundle に依存するコードを動作させたい場合は、それを次のように置き換えることができます。

[NSBundle bundleForClass: [self class]]

これが効率が悪いかどうかはわかりませんが (おそらく)、単体テストでは機能します。

2 つ目の問題は、単体テストがサンド ボックス内で実行されていないため、ドキュメント ディレクトリが正しく参照されないことです。これを修正する方法はいくつかありますが、最も簡単な方法は、~/Library/Application Support/iPhone Simulator/6.1/ にドキュメント ディレクトリを作成することです。明らかに、6.1 は単なる例です。

参考文献:-

OCUnit & NSBundle

iPhone単体テストのNSHomeDirectory

ところで、私は古い質問に答えるのが大好きです。

于 2013-08-29T09:35:45.997 に答える