2016-08-25 58 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; 
} 
相關問題