我正在使用 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 运行没有任何问题,我只是无法让它们从命令行工作。 有任何想法吗? 谢谢
最佳答案
我在单元测试和核心数据方面遇到了同样的问题:
首先我遇到了“找不到商店模型”。我使用此线程修复了此问题:Error running 'Cedar' unit tests from command line
其次,我遇到了同样的错误:“创建文件失败;代码 = 2”
然后我查看了 URL 并看到:
NSPersistentStoreCoordinator 和 NSManagedObjectModel 的 URL 非常不同。但只有当我进行单元测试并且只有在修复第一个错误之后。
通常模型在“/some/path/
sqlite 在 "some/path/Documents/
因此我使用以下代码在测试和运行中获取正确的 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 文件的“Documents”文件夹。
如果这对您有用或如何做得更好,我们将不胜感激。
关于ios - 从命令行运行 'Cedar' 单元测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12363319/