2015-12-11 213 views
0

自動化測試我想爲我的核心數據模型遷移自動化測試。我有模型的三個版本 - 1.0,1.1和1.2。我想創建一個新的數據庫,每一個測試,用假數據填充它,比它遷移到較新的版本,測試一路走來的任何錯誤。我應該如何處理這樣的寫作測試?的核心數據遷移

回答

0

我已經加載模型從mom而不是momd文件解決了這個。例如測試看起來是這樣的:

func testMigarationFrom_1_0_To_1_1() { 
    let modelUrl = NSBundle.mainBundle().URLForResource("1.0", withExtension: "mom", subdirectory: "Model.momd")! 
    let model = NSManagedObjectModel(contentsOfURL: modelUrl)! 
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model) 
    let databaseUrl = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("Storage").URLByAppendingPathExtension("sqlite") 
    try! coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: databaseUrl, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]) 

    let context = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) 
    context.persistentStoreCoordinator = coordinator 

    for i in 0..<10 { 
     let entity = NSEntityDescription.insertNewObjectForEntityForName("entity", inManagedObjectContext: context) as! Entity 
     entity.name = "test-\(i)" 
    } 

    let newModelUrl = NSBundle.mainBundle().URLForResource("1.1", withExtension: "mom", subdirectory: "Model.momd")! 
    let newModel = NSManagedObjectModel(contentsOfURL: newModelUrl)! 
    let newCoordinator = NSPersistentStoreCoordinator(managedObjectModel: newModel) 

    do { 
     try newCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: databaseUrl, options: [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]) 
    } catch let error { 
     XCTFail("Should migrate without error, got \(error)") 
    } 
}