首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >核心数据迁移的自动化测试

核心数据迁移的自动化测试
EN

Stack Overflow用户
提问于 2015-12-11 19:21:32
回答 1查看 111关注 0票数 0

我想为我的核心数据模型迁移创建自动化测试。我有三个版本的模型- 1.0,1.1和1.2。我想用每个测试创建一个新的数据库,用假数据填充它,而不是将它迁移到一个更新的版本中,测试整个过程中的任何错误。我应该如何处理这样的写作测试呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-11 19:55:33

我通过从mom而不是momd文件加载模型来解决这个问题。示例测试如下所示:

代码语言:javascript
复制
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)")
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34231012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档