2016-08-25 59 views
0

我创建了一个框架项目来包含我的应用程序的所有业务逻辑。因此,核心数据模型也转移到了框架项目中。现在我有几个XCTestCase类来为框架中的管理器和模块类进行单元测试。这些类具有核心数据操作。因此,在单元测试期间,这些类引用DBManager类。在这种情况下,我正在发生崩溃,发生在创建持久性模型时。同时,当我在主项目中使用相同的单元测试用例时,一切正常。我主要提到的一件事是,持久创建时创建的storeURL在运行主项目和单元测试用例时不同该框架。以下是创建的storeURL。 file:/// Users/Gowtham/Library/Developer/CoreSimulator/Devices/F7DB4F9B-D323-4978-A816-B5363F26BE32/data/Containers/Data/Application/92F08534-F5AD-4B4B-B1A3-D3CEF17758C0/Documents/IHA。 sqlite的[主要项目]从框架项目进行单元测试时应用程序崩溃

文件:///Users/Gowtham/Library/Developer/CoreSimulator/Devices/F7DB4F9B-D323-4978-A816-B5363F26BE32/data/Documents/IHA.sqlite [单元测试用例框架项目]

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

未解决的错误错误域= YOUR_ERROR_DOMAIN代码= 9999 “无法初始化应用程序的保存数据” 的UserInfo = {NSLocalizedDescription =无法初始化 应用程序保存的数据,NSUnderlyingError = 0x7be93420 {错误 域= NSCocoaErrorDomain代码(空)“UserInfo = {元数据= {N/A} {NSPersistenceFrameworkVersion = 641; NSStoreModelVersionHashes = { UserDictionaryEntry =; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers =( “” ); NSStoreType = SQLite; NSStoreUUID =“6C70A93B-39DD-4357-A0C2-0BDA5DF51E32”; “_NSAutoVacuumLevel”= 2; },reason =用于打开商店的模型与用于创建商店的模型不兼容}}, NSLocalizedFailureReason =创建或加载应用程序的保存数据时发生错误},{ NSLocalizedDescription =“无法初始化应用程序保存的数据“; NSLocalizedFailureReason =“创建或加载应用程序保存的数据时发生错误。”; NSUnderlyingError = “错误域= NSCocoaErrorDomain代码= 134100 \”(空)\”的UserInfo = {元数据= {\ 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 =用于打开商店的模型与用于创建商店的模型不兼容}“; }

谁能给我编写单元测试用例与核心数据在Framework项目沟通的正确方法的建议吗?

崩溃是发生在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调用数据库操作时,崩溃正在发生。请求任何人提供有用的答案。

回答

0

感谢您给出的建议。最后,我发现了很简单的原因。尽管我已经从模拟器中删除了应用程序,但在以下路径中创建的sqlite文件的引用未被清除。然后我手动去了那个路径并删除了生成的旧的sqlite文件。然后一切正常。如果有人面临类似的问题,请检查从以下路径生成的较旧的sqlite文件。在较旧的sqlite和xcdatamodel中定义的结构中存在不匹配。

/Users/...username.../Library/Developer/CoreSimulator/Devices/F7DB4F9B-D323-4978-A816-B5363F26BE32/data/Documents/appname.sqlite

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

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; 
} 
相关问题