-
-
Save Rodolfocartas/4229346 to your computer and use it in GitHub Desktop.
Bootstrap a CoreData test class
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
| #import <SenTestingKit/SenTestingKit.h> | |
| #import <CoreData/CoreData.h> | |
| @interface Test : SenTestCase { | |
| NSManagedObjectModel *model; | |
| NSPersistentStoreCoordinator *coordinator; | |
| NSManagedObjectContext *context; | |
| } | |
| - (NSString *)applicationSupportDirectory; | |
| @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
| #import "Test.h" | |
| @implementation Test | |
| - (void)setUp { | |
| model = [[NSManagedObjectModel mergedModelFromBundles: | |
| [NSArray arrayWithObject:[NSBundle bundleWithIdentifier:@"com.yourcompany.test"]]] retain]; | |
| coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; | |
| context = [[NSManagedObjectContext alloc] init]; | |
| [context setPersistentStoreCoordinator:coordinator]; | |
| NSFileManager *fileManager = [NSFileManager defaultManager]; | |
| NSString *applicationSupportDirectory = [self applicationSupportDirectory]; | |
| NSError *error = nil; | |
| if ( ![fileManager fileExistsAtPath:applicationSupportDirectory isDirectory:NULL] ) { | |
| if (![fileManager createDirectoryAtPath:applicationSupportDirectory withIntermediateDirectories:NO attributes:nil error:&error]) { | |
| NSAssert(NO, ([NSString stringWithFormat:@"Failed to create App Support directory %@ : %@", applicationSupportDirectory,error])); | |
| NSLog(@"Error creating application support directory at %@ : %@",applicationSupportDirectory,error); | |
| } | |
| } | |
| NSURL *storeUrl = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"test"]]; | |
| [fileManager removeItemAtPath:storeUrl.path error:&error]; | |
| if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { | |
| NSLog(@"Error creating store: %@", error); | |
| } | |
| } | |
| - (NSString *)applicationSupportDirectory { | |
| NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); | |
| NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory(); | |
| return [basePath stringByAppendingPathComponent:@"test"]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment