Search code examples
swiftcore-datapackage-managersxcode11

Swift package unit testing with CoreData


I have a swift bunch of files that provides some services above CoreData. I had unit tests running fine. I decided to move all this into a Swift Package using XCode 11.

The unit tests don't run anymore, having a runtime error of caught "NSInternalInconsistencyException", "NSFetchRequest could not locate an NSEntityDescription for entity name.

To do my unit tests, I have a xcdatamodel file that is included in the test group if the package. But it seems XCode is not allowing me to really include this xcdatamodel file as part of the test target.

How can I include this xcdatamodel in the test target ?


Solution

  • According to this post, .xcdatamodeld files aren't supported because you can't bundle resources, so if you want to use Core Data models you have to do it programmatically (shown how to in the post).

    P.S. I'm in the process of figuring this out myself too and I think the other thing to keep in mind will be specifying managedObjectModel (in addition to name) if/when instantiating NSPersistentContainer.