0

I'm creating the entity in the production code like this:

    let newDevice = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context) as! Device

Then I'm using this class in unit tests but get this runtime error on this line:

Could not cast value of type 'Umando.Device' (0x6080000b6570) to 'UmandoTests.Device' (0x1151ab5c0).

Looks like XCode assigns different class names for different modules but then this should be some obvious known issue, yet I found no solution for that so far.

UPDATE: Code for unit text context init (adapted from someone's):

    lazy var managedObjectModel: NSManagedObjectModel = {
    let managedObjectModel = NSManagedObjectModel.mergedModel(from: [Bundle(for: type(of: self))] )!
    return managedObjectModel
}()

lazy var mockPersistantContainer: NSPersistentContainer = {

    let container = NSPersistentContainer(name: "PersistentDeviceList", managedObjectModel: self.managedObjectModel)
    let description = NSPersistentStoreDescription()
    description.type = NSInMemoryStoreType
    description.shouldAddStoreAsynchronously = false // Make it simpler in test env

    container.persistentStoreDescriptions = [description]
    container.loadPersistentStores { (description, error) in
        // Check if the data store is in memory
        precondition( description.type == NSInMemoryStoreType )

        // Check if creating container wrong
        if let error = error {
            fatalError("Create an in-mem coordinator failed \(error)")
        }
    }
    return container
}()
Arnie Schwarzvogel
  • 955
  • 1
  • 13
  • 25

0 Answers0