4

I'm using the cedar testing framework and trying to run the tests from command line. The build crashes with this error:

Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x6c5c6c0 {reason=Failed to create file; code = 2}, {
reason = "Failed to create file; code = 2";
}

The tests run from xcode without any problems, I just can't get them to work from command line. Any ideas? Thanks

4

1 回答 1

2

我对单元测试和核心数据有同样的问题:

首先我遇到了“找不到商店的模型”。我使用这个线程解决了这个问题:Error running 'Cedar' unit tests from command line

其次,我遇到了同样的错误:“无法创建文件;代码 = 2”

然后我查看了 URL 并看到:

NSPersistentStoreCoordinator 和 NSManagedObjectModel 的 URL 完全不同。但只有当我进行单元测试并且只有在修复第一个错误之后。

通常模型在 "/some/path/<DataModelFile>.app/<DataModel>.momd/
sqlite 在 "some/path/Documents/<SQLiteFile>.sqlite

因此,我使用以下代码在测试和运行中获取正确的 URL:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *modelURL = [bundle URLForResource:@"WinNav" withExtension:@"momd"];

basePath = @"";
NSArray *arr = [modelURL pathComponents];
for (int i=0; i<[arr count]-2; i++) {
    basePath = [basePath stringByAppendingString:[arr objectAtIndex:i]];
    if (i > 0) basePath = [basePath stringByAppendingString:@"/"];
}
NSLog(@"modelURL: %@", modelURL);

NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingString:@"/Documents/Locations.sqlite"]];

如果我没记错的话,我必须创建存储 sqlite 文件的“文档”文件夹。

如果这对您有用或如何做得更好,我们将不胜感激。

于 2012-10-11T08:12:51.420 回答