objective-c - NSManagedObjectModel 使用 mergedModelFromBundles :nil not finding model in SenTestCase 初始化

标签 objective-c ios core-data sentestingkit

我正在尝试在 SenTestCase 中使用 CoreData。问题是找不到 NSManagedObjectModel。我尝试通过在应用程序包中搜索 mom 文件来使用 URL 来初始化它,但找不到它。这就是我切换到的原因:

NSManagedObjectModel *objectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

这确实可以正常工作,但仅限于主应用程序。如果我尝试在 SenTestCase 中运行它,返回的 objectModel 没有任何实体:

(gdb) po objectModel
(<NSManagedObjectModel: 0xab72480>) isEditable 0, entities {
}, fetch request templates {
}

我必须承认,我不知道生成 mom 文件需要什么样的设置。我已将 .xcdatamodeld 文件添加到应用程序目标和测试目标中的已编译源列表中。

以下代码在应用程序 ViewController viewDidLoad 方法中正常工作,但在 SenTestCase 类的测试用例中不起作用:

- (void)testCoreData {
    NSManagedObjectModel *objectModel = [NSManagedObjectModel mergedModelFromBundles:nil];


    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];


    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: objectModel];
    [context setPersistentStoreCoordinator: coordinator];

    NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    url = [url URLByAppendingPathComponent:@"TestDatabase"];

    NSLog(@"Path: %@", url);

    NSError *error;

    NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                        configuration:nil
                                                                  URL:url
                                                              options:nil
                                                                error:&error];

    if (newStore == nil) {
        NSLog(@"Store Configuration Failure\n%@",
              ([error localizedDescription] != nil) ?
              [error localizedDescription] : @"Unknown Error");
    }

    TaskSet *taskSet = [NSEntityDescription insertNewObjectForEntityForName:@"TaskSet" 
                                                 inManagedObjectContext:context];
    taskSet.taskSetId = @"1234";

    NSLog(@"TaskSet: %@", taskSet);
    NSError *saveError;
    if ([context save:&saveError]) {
        NSLog(@"Store Saved successfully");
    } else {
        NSLog(@"Store not saved! Error: %@", saveError);
    }
}

错误:

Catchpoint 3 (exception thrown).Unknown.m:0: error: -[ControllerTest testCoreData] : +entityForName: could not locate an entity named 'TaskSet' in this model.

在 SenTestCase 中运行和在 viewDidLoad 中运行有什么区别?我需要做什么才能将核心数据模型编译成测试用例?

最佳答案

这个问题之前已经回答过:Unit Test can't find Core Data model file

这要归功于 Luther Baker 指出 SenTestCases 没有使用主 Bundle。如果您想将数据写入文件,您必须创建带有标识符的包,如他的答案中所述。

关于objective-c - NSManagedObjectModel 使用 mergedModelFromBundles :nil not finding model in SenTestCase 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187496/

相关文章:

swift - SwiftUI View和@FetchRequest谓词带有可以更改的变量

ios - 摆脱旧模型版本中的核心数据警告

objective-c - 禁止 NSOpenPanel 中的特定文件类型

ios - 在 iOS 中选择内置麦克风和耳机

ios - 单击 Collection View 单元格时如何获取 TableView 部分

iOS 通知 : How does WhatsApp receive notification and process even after force closing them?

ios - 为什么弱强舞会保持循环?

swift - ObjectMapper 如何在嵌套实体中映射值

iphone - 如何在 iPhone 中检测到达的短信?

ios - NSCoding 和 NSData 是什么关系?