Swift cannot test core data in Xcode tests?

前端 未结 5 623
失恋的感觉
失恋的感觉 2021-02-02 12:42

I am working on a iOS project that uses core data. I am using swift. The Core Data stack is setup right and all seems to be fine. I have created a class for an entity (NSManage

5条回答
  •  [愿得一人]
    2021-02-02 13:36

    I think I'm getting similar results to you. I was unable to get my tests working with the line

    var newDept = NSEntityDescription.insertNewObjectForEntityForName("Department", inManagedObjectContext: moc) as Department
    

    But I could get the tests running with :

    let entity = NSEntityDescription.entityForName("Department", inManagedObjectContext: moc)
    let department = Department(entity: entity!, insertIntoManagedObjectContext: moc)
    

    My Entity looks like :

    @objc(Department)
    class Department: NSManagedObject {
    
        @NSManaged var department_description: String
        ...
    }
    

提交回复
热议问题