Last active
August 29, 2015 14:20
-
-
Save sprohaszka/d9a73a7421936acfcf79 to your computer and use it in GitHub Desktop.
Adding CoreData to XCTest
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
@implementation XCTestCase(CoreData) | |
+ (id)fakeEntityForTests:(Class)clazz { | |
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:NSStringFromClass(clazz) | |
inManagedObjectContext:[[self class] managedObjectContextForTests]]; | |
return [[clazz alloc] initWithEntity:entityDesc | |
insertIntoManagedObjectContext:[[self class] managedObjectContextForTests]]; | |
} | |
+ (NSManagedObjectContext *)managedObjectContextForTests { | |
static NSManagedObjectModel *model = nil; | |
if (!model) { | |
model = [NSManagedObjectModel mergedModelFromBundles:[NSBundle allBundles]]; | |
} | |
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; | |
NSPersistentStore *store = [psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]; | |
NSAssert(store, @"Should have a store by now"); | |
// To keep a unique Context ! | |
static NSManagedObjectContext *moc = nil; | |
if (!moc) { | |
moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; | |
moc.persistentStoreCoordinator = psc; | |
} | |
return moc; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment