Skip to content

Instantly share code, notes, and snippets.

@sprohaszka
Last active August 29, 2015 14:20
Show Gist options
  • Save sprohaszka/d9a73a7421936acfcf79 to your computer and use it in GitHub Desktop.
Save sprohaszka/d9a73a7421936acfcf79 to your computer and use it in GitHub Desktop.
Adding CoreData to XCTest
@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