Skip to content

Instantly share code, notes, and snippets.

@Rodolfocartas
Forked from siannopollo/Test.h
Created December 6, 2012 23:17
Show Gist options
  • Select an option

  • Save Rodolfocartas/4229346 to your computer and use it in GitHub Desktop.

Select an option

Save Rodolfocartas/4229346 to your computer and use it in GitHub Desktop.
Bootstrap a CoreData test class
#import <SenTestingKit/SenTestingKit.h>
#import <CoreData/CoreData.h>
@interface Test : SenTestCase {
NSManagedObjectModel *model;
NSPersistentStoreCoordinator *coordinator;
NSManagedObjectContext *context;
}
- (NSString *)applicationSupportDirectory;
@end
#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