Reputation: 703
I'm writing a swift framework and I'm using a template from 'pod lib create' command. in my development pods, I've created a core data model file and I'm using core data in my framework. now I want to write unit tests for it. this is my unit test class code:
import XCTest
import CoreData
@testable import MyFramework
class MyClassTests: XCTestCase {
var testPersistentContainer: NSPersistentContainer?
override func setUp() {
let persistentStoreDescription = NSPersistentStoreDescription()
persistentStoreDescription.type = NSInMemoryStoreType
let container = NSPersistentContainer(name: "MyCoreDataModelFileName")
container.persistentStoreDescriptions = [persistentStoreDescription]
container.loadPersistentStores { (storeDescription, error) in
if let error = error {
fatalError(error.localizedDescription)
}
}
self.testPersistentContainer = container
}
override func tearDown() {
}
func testSomething() {
// the persistent container I'm trying to use is nil
XCTAssertNotNil(self.testPersistentContainer)
}
}
I know what is the problem, the problem is that the test target can't find my core data model file, and when I create a file with a proper name in my example app target, the above test passes. but
Question
What is the correct way of testing the core data model of a framework?
Upvotes: 4
Views: 772
Reputation: 52053
Have you tried loading the Core Data model from the framework bundle?
let managedObjectModel = NSManagedObjectModel.mergedModel(from: nil)
Then you won't need to add it to your test target
Upvotes: 1