Created
October 2, 2015 12:29
-
-
Save RawToast/b484d70757211ccde686 to your computer and use it in GitHub Desktop.
In memory CoreData configuration for unit testing
This file contains 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
let context = setUpInMemoryManagedObjectContext()
let myObject = NSEntityDescription.insertNewObjectForEntityForName("MyObject", inManagedObjectContext: context) as! MyObject