我创建了一个框架项目来包含我的应用程序的所有业务逻辑。因此,核心数据模型也转移到了框架项目中。现在我有几个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调用数据库操作时,崩溃正在发生。请求任何人提供有用的答案。