How to give unit tests access to Core Data models

China☆狼群 提交于 2019-12-24 05:49:12

问题


Let's say I've made Core Data models in my project. I have other classes with methods that take in these models and perform some function based on their properties. How do I unit test those classes?

With normal Swift files I select them in Xcode and tick a box which makes any classes defined in those files visible to the unit test part of the project. My question is essentially, how do I make my Core Data models also visible to the tests?

Note that I don't want to perform any operations on the Core Data stack, I just want to be able to create an instance of a model and pass it into a method.


回答1:


Since CoreData uses Managed Objects, any subclass of NSManagedObject is functionally worthless unless attached to a context.

One trick for testing is to create an NSManagedObjectContext in memory and create objects test using that context. Outlined Here

Here's the code for creating the context in memory:

func setUpInMemoryManagedObjectContext() -> NSManagedObjectContext {
    let managedObjectModel = NSManagedObjectModel.mergedModelFromBundles([NSBundle.mainBundle()])!

    let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)

    do {
        try persistentStoreCoordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil)
    } catch {
        print("Adding in-memory persistent store failed")
    }

    let managedObjectContext = NSManagedObjectContext()
    managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator

    return managedObjectContext
}



回答2:


Update: The accessor implementations are generated at runtime, but they are not usable if the model object is not associated with a NSManagedObjectContext. So PEEJWEEJ's answer is the way to go.

If I am not mistaken, the Objective-C classes for your entities (in particular e.g. their property accessors) are generated at runtime, when they are used. So I would expect that you'd need to call NSManagedObjects initializer with the appropriate NSEntityDescription:

NSManagedObject(entity: entityDescription, insertInto: nil)

You can get the appropriate entity descriptions by loading your object model with NSManagedObjectModel and using e.g. that object's entitiesByName property.

If you use custom code for your NSManagedObject subclasses (e.g. generated with mogenerator), I would assume the same process would need to apply - make the generated code visible to your tests and use the MyModelClass(entity: entityDescription, insertInto: nil) initializer.



来源:https://stackoverflow.com/questions/41985415/how-to-give-unit-tests-access-to-core-data-models

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!