ios - 从命令行运行 'Cedar' 单元测试时出错

标签 ios xcode unit-testing cedar-bdd

我正在使用 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/.app/.momd/
sqlite 在 "some/path/Documents/.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 文件的“Documents”文件夹。

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

关于ios - 从命令行运行 'Cedar' 单元测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12363319/

相关文章:

ios - 警告 : Ambiguous Content: The app icon set "AppIcon" has an unassigned child (editor->xcode 10. 0)

ios - Swift:在委托(delegate)方法中使用 presentViewController 时为 "Attempt to present ViewController whose view is not in the window hierarchy"

ios - Visual Studio Cordova IOS 远程构建失败

javascript - 开 Jest - 如何测试 console.error 被调用?

java - 我有一段代码,即带有 try 和 catch block 的方法。 catch block 捕获异常,我该如何为其编写失败 junit?

ios - 如何在 Swift 上使这段代码异步

c# - StyledStringElement 'Tapped' 事件

ios - AVPlayer 流媒体延迟

ios - 无需开发帐户即可从 Xcode 部署应用程序

python - 如何对读取命令行参数的类进行单元测试?