从框架项目

时间:2016-08-25 11:15:17

标签: ios objective-c unit-testing core-data

我创建了一个框架项目来包含我的应用程序的所有业务逻辑。因此,核心数据模型也转移到了框架项目。现在我有一些XCTestCase类来为框架中的管理器和模块类进行单元测试。这些类具有核心数据操作。因此,在单元测试期间,这些类指的是DBManager类。我在这种情况下遇到崩溃,在创建持久模型时发生。在我在主项目中使用相同的单元测试用例的同时,一切都运行正常。我主要注意的一点是,在持久创建时创建的storeURL在运行主项目和单元测试用例时是不同的。框架。以下是storeURL创建。 文件:///用户/的Gowtham /库/开发商/ CoreSimulator /设备/ F7DB4F9B-D323-4978-A816-B5363F26BE32 /数据/容器/数据/应用/ 92F08534-F5AD-4B4B-B1A3-D3CEF17758C0 /文档/ IHA.sqlite [主项目]

file:///Users/Gowtham/Library/Developer/CoreSimulator/Devices/F7DB4F9B-D323-4978-A816-B5363F26BE32/data/Documents/IHA.sqlite [框架项目中的单元测试用例] < / strong>

以下是控制台中显示的异常。

  

未解决的错误错误域= YOUR_ERROR_DOMAIN代码= 9999“无法初始化应用程序的已保存数据”   UserInfo = {NSLocalizedDescription =无法初始化   应用程序保存的数据,NSUnderlyingError = 0x7be93420 {错误   Domain = NSCocoaErrorDomain Code = 134100“(null)”UserInfo = {metadata = {       NSPersistenceFrameworkVersion = 641;       NSStoreModelVersionHashes = {           UserDictionaryEntry =;       };       NSStoreModelVersionHashesVersion = 3;       NSStoreModelVersionIdentifiers =(           “”       );       NSStoreType = SQLite;       NSStoreUUID =“6C70A93B-39DD-4357-A0C2-0BDA5DF51E32”;       “_NSAutoVacuumLevel”= 2; },reason =用于打开商店的模型与用于创建商店的模型不兼容}},   NSLocalizedFailureReason =创建或加载时出错   应用程序保存的数据。},{       NSLocalizedDescription =“无法初始化应用程序保存的数据”;       NSLocalizedFailureReason =“创建或加载应用程序保存的数据时出错。”;       NSUnderlyingError =“Error Domain = NSCocoaErrorDomain Code = 134100 \”(null)\“UserInfo = {metadata = {\ n NSPersistenceFrameworkVersion =   641; \ n NSStoreModelVersionHashes = {\ n
  UserDictionaryEntry =; \ n}; \ n
  NSStoreModelVersionHashesVersion = 3; \ n
  NSStoreModelVersionIdentifiers =(\ n \“\”\ n); \ n
  NSStoreType = SQLite; \ n NSStoreUUID =   \“6C70A93B-39DD-4357-A0C2-0BDA5DF51E32 \”; \ n \“_ NSAutoVacuumLevel \”   = 2; \ n},reason =用于打开商店的模型与用于创建商店的模型不兼容}“;}

任何人都可以建议我在框架项目中编写与核心数据进行通信的单元测试用例的正确方法吗?

崩溃发生在DatabaseManager类的以下方法中:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
}

// Create the coordinator and store

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"IHA.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    // Report any error we got.
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
    dict[NSLocalizedFailureReasonErrorKey] = failureReason;
    dict[NSUnderlyingErrorKey] = error;
    error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
    // Replace this with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator;

}

当应用程序与Framework项目进行数据库操作通信时,它运行正常。同时,当框架项目中的XCTest Target调用数据库操作时,崩溃正在发生。请求任何人提供有用的答案。

2 个答案:

答案 0 :(得分:1)

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil)
    {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"IHA.sqlite"];

    NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _persistentStoreCoordinator;
}

答案 1 :(得分:0)

感谢您给出的建议。最后我发现了非常简单的原因。即使我已经从模拟器删除了应用程序,之前在以下路径中创建的sqlite文件的引用也没有被清除。然后我手动转到该路径并删除生成的旧sqlite文件。一切正常。如果有人遇到类似的问题,请检查从以下路径生成的较旧的sqlite文件。旧版sqlite和xcdatamodel中定义的结构不匹配。

/用户/ ...用户名... /库/开发商/ CoreSimulator /设备/ F7DB4F9B-D323-4978-A816-B5363F26BE32 /数据/文档/ appname.sqlite

F7DB4F9B-D323-4978-A816-B5363F26BE32是保存与应用程序相关的所有数据的文件夹。