Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
In memory CoreData configuration for unit testing
import Foundation
import CoreData
func setUpInMemoryManagedObjectContext() -> NSManagedObjectContext {
let managedObjectModel = NSManagedObjectModel.mergedModelFromBundles([NSBundle.mainBundle()])!
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
do {
try persistentStoreCoordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: nil, URL: nil, options: nil)
} catch {
assertionFailure("Unable to setup coredata")
}
let managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator
return managedObjectContext
}
@RawToast
Copy link
Author

RawToast commented Oct 2, 2015

Usage:

let context = setUpInMemoryManagedObjectContext()
let myObject = NSEntityDescription.insertNewObjectForEntityForName("MyObject", inManagedObjectContext: context) as! MyObject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment