使用示例Core Data进行单元测试

时间:2015-07-01 14:19:51

标签: ios swift unit-testing core-data nsmanagedobjectcontext

这是我的XCTestCase类的开始:

var moc: NSManagedObjectContext!

    override func setUp() {
        super.setUp()

        moc = self.setUpInMemoryManagedObjectContext()
        self.fillManagedObjectContextWithExampleData(moc)
    }

    func setUpInMemoryManagedObjectContext() -> NSManagedObjectContext {

        let modelName = "ProjectApp"
        let modelURL = NSBundle.mainBundle().URLForResource(modelName, withExtension:"momd")!
        let managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)!

        let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
        persistentStoreCoordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil, error: nil)

        let managedObjectContext = NSManagedObjectContext()
        managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator

        return managedObjectContext
    }

    func fillManagedObjectContextWithExampleData(context: NSManagedObjectContext) {
        // var firstSC = NSEntityDescription..insertNewObjectForEntityForName("StaticContent", inManagedObjectContext: context) as! StaticContent
        var staticContentEntity = NSEntityDescription.entityForName("StaticContent", inManagedObjectContext: context)!

        var firstSC = StaticContent(entity: staticContentEntity, insertIntoManagedObjectContext: context)
        firstSC.name = "First Name"

        var secondSC = StaticContent(entity: staticContentEntity, insertIntoManagedObjectContext: context)
        secondSC.name = "Second Name"

        var error: NSError? = nil

        if context.save(&error) {
            return
        }
    }

我只想创建managedObjectContext(在内存中,仅用于测试)并用示例数据填充它。所以我可以使用:

managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as! [StaticContent]

在我的单元测试中。它执行但是当我调用带[StaticContent]的函数时出现错误:

fatal error: NSArray element failed to match the Swift Array Element type

这有什么问题?我的功能我称之为正常。当我在我的应用程序中使用它而不是在单元测试中我没有问题。那么我做错了什么?

2 个答案:

答案 0 :(得分:1)

我真的不喜欢回答我的问题,但我找到了答案,我想要帮助其他人。我找到了一部分代码,它根据运行的目标来更改实体类名。所以我在创建NSManagedObjectModel的地方添加了这些代码行,这有助于:

    // Create the module name
    let moduleName = "ProjectAppTests"

    // Create a new managed object model with updated entity class names
    var newEntities = [] as [NSEntityDescription]
    for (_, entity) in enumerate(managedObjectModel.entities) {
        let newEntity = entity.copy() as! NSEntityDescription
        newEntity.managedObjectClassName = "\(moduleName).\(entity.name)"
        newEntities.append(newEntity)
    }
    let newManagedObjectModel = NSManagedObjectModel()
    newManagedObjectModel.entities = newEntities

    let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: newManagedObjectModel)

不知道为什么使用Swift在Core Data中使用类名这么复杂。但它有所帮助,现在正在发挥作用。

答案 1 :(得分:0)

在swift 3.0中:

func setUpInMemoryManagedObjectContext() -> NSManagedObjectContext {
        let moduleName = "Model"
        do {
            let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
            let modelURL = Bundle.main.url(forResource: moduleName, withExtension:"momd")
            let newObjectModel = NSManagedObjectModel.init(contentsOf: modelURL!)
            let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel : newObjectModel!)
            try  persistentStoreCoordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: nil, options: nil)

            managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator
            return managedObjectContext
        } catch let error as NSError {
            logErr(error.localizedDescription)
            XCTFail()
        }
        XCTFail("failed to created mocked coreData (inMemroy)")
        return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    } 
func fillManagedObjectContextWithExampleData(context: NSManagedObjectContext) {
//lets assume you got some Food POJO class
    do {
        let foodJson: NSDictionary = ["id" : "1" , "foodId" : "1" , "displayName": "Pizza"]
        let firstFood = try Food.createOrUpdate(dict: foodJson, inManagedObjectContext: context)
        try  context.save()
        return
    } catch let error as NSError {
        logErr(error.localizedDescription)
    }
}

func setUpInMemoryManagedObjectContext() -> NSManagedObjectContext { let moduleName = "Model" do { let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) let modelURL = Bundle.main.url(forResource: moduleName, withExtension:"momd") let newObjectModel = NSManagedObjectModel.init(contentsOf: modelURL!) let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel : newObjectModel!) try persistentStoreCoordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: nil, options: nil) managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator return managedObjectContext } catch let error as NSError { logErr(error.localizedDescription) XCTFail() } XCTFail("failed to created mocked coreData (inMemroy)") return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) } func fillManagedObjectContextWithExampleData(context: NSManagedObjectContext) { //lets assume you got some Food POJO class do { let foodJson: NSDictionary = ["id" : "1" , "foodId" : "1" , "displayName": "Pizza"] let firstFood = try Food.createOrUpdate(dict: foodJson, inManagedObjectContext: context) try context.save() return } catch let error as NSError { logErr(error.localizedDescription) } }