Last active
August 29, 2015 14:05
-
-
Save dmiedema/08795a96af5a9ce458a2 to your computer and use it in GitHub Desktop.
Create in memory NSManagedObjectContext for testing classes that use CoreData.
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
| // | |
| // DMMTestUtilities.h | |
| // dmiedema | |
| // | |
| // Created by Daniel on 8/25/14. | |
| // Copyright (c) 2014 Daniel Miedema. All rights reserved. | |
| // | |
| #import <Foundation/Foundation.h> | |
| @interface DMMTestUtilities : NSObject | |
| /*! | |
| Create and return an in memory @c NSManagedObjectContext for testing | |
| Each call will return a new @c NSManagedObjectContext. Call with caution. | |
| @return NSManagedObjectContext with an @c NSInMemoryStoreType for testing | |
| */ | |
| + (NSManagedObjectContext *)inMemoryManagedObjectContext; | |
| @end |
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
| // | |
| // DMMTestUtilities.m | |
| // dmiedema | |
| // | |
| // Created by Daniel on 8/25/14. | |
| // Copyright (c) 2014 Daniel Miedema. All rights reserved. | |
| // | |
| #import "DMMTestUtilities.h" | |
| @interface DMMTestUtilities() | |
| @property (strong, nonatomic) NSManagedObjectModel *managedObjectModel; | |
| @property (strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
| @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext; | |
| @end | |
| @implementation DMMTestUtilities | |
| + (NSManagedObjectContext *)inMemoryManagedObjectContext { | |
| DMMTestUtilities *testHelper = [[DMMTestUtilities alloc] init]; | |
| NSURL *url = [[NSBundle mainBundle] URLForResource:@"sqlite" withExtension:@"momd"]; | |
| testHelper.managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url]; | |
| testHelper.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:testHelper.managedObjectModel]; | |
| if (![testHelper.persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]) { | |
| return nil; | |
| } | |
| testHelper.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; | |
| testHelper.managedObjectContext.persistentStoreCoordinator = testHelper.persistentStoreCoordinator; | |
| return testHelper.managedObjectContext; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment