从命令行运行'Cedar'单元测试时出现错误

4

我正在使用Cedar测试框架并尝试从命令行运行测试。构建过程中出现以下错误:

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

测试在Xcode中没有任何问题,但是我无法通过命令行让它们工作。你有什么想法吗?谢谢。
1个回答

2

我在进行单元测试和Core Data时遇到了同样的问题:

首先,我遇到了“无法找到存储模型”的问题。我通过这个主题进行了修复: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"]];

如果我没记错的话,我需要创建“Documents”文件夹,这里存储了sqlite文件。
如果您有任何评论/建议,可以让我知道是否有效或如何更好地完成。

这对我有用,谢谢。小细节,需要在Documents / Locations.sqlite前面加上/。 - ggfela

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接