Created
February 27, 2019 16:07
-
-
Save novinfard/dd13ef2440fb4cda877bbb211ae00ec3 to your computer and use it in GitHub Desktop.
[differences between loading persistent container within the app or unit test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inside the app (SQLite) | |
lazy var persistentContainer: NSPersistentContainer = { | |
let container = NSPersistentContainer(name: "CoreDataUnitTesting") | |
container.loadPersistentStores { (description, error) in | |
if let error = error { | |
fatalError("Unresolved error \(error), \(error.userInfo)") | |
} | |
} | |
return container | |
}() | |
// Inside the test (in-memory) | |
lazy var mockPersistantContainer: NSPersistentContainer = { | |
let container = NSPersistentContainer(name: "CoreDataUnitTesting", managedObjectModel: self.managedObjectModel) | |
let description = NSPersistentStoreDescription() | |
description.type = NSInMemoryStoreType | |
description.shouldAddStoreAsynchronously = false | |
container.persistentStoreDescriptions = [description] | |
container.loadPersistentStores { (description, error) in | |
precondition(description.type == NSInMemoryStoreType) | |
if let error = error { | |
fatalError("In memory coordinator creation failed \(error)") | |
} | |
} | |
return container | |
}() | |
var managedObjectModel: NSManagedObjectModel = { | |
let managedObjectModel = NSManagedObjectModel.mergedModel(from: [Bundle.main])! | |
return managedObjectModel | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment