【问题标题】:Error running 'Cedar' unit tests from command line从命令行运行“Cedar”单元测试时出错
【发布时间】:2012-09-11 05:36:00
【问题描述】:

我正在使用 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 运行没有任何问题,我只是无法让它们从命令行运行。 有任何想法吗? 谢谢

【问题讨论】:

    标签: ios xcode unit-testing cedar-bdd


    【解决方案1】:

    我在单元测试和核心数据方面遇到了同样的问题:

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

    第二次出现同样的错误:“无法创建文件;代码 = 2”

    然后我查看了网址,发现:

    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”文件夹。

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

    【讨论】:

    • 这对我有用,谢谢。小细节,需要在Documents/Locations.sqlite前面加上不带/的。
    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2011-04-25
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2018-06-18
    相关资源
    最近更新 更多