Reputation: 65409
For my app i'm using a MVC-Store model.
The class DataStore holds a singleton object defaultstore which holds the data for the app. Everything works out fine but i got some problems with my unit tests because i don't want my tests mess up the data.
I would like to do the following but i don't know how:
- (void)setUp
{
[super setUp];
//Backup my data
//Clear coredata
}
- (void) testCreateSomeData
{
//..Create some data, add it to the store, do some tests, etc..
}
- (void)tearDown
{
Put back my original data
[super tearDown];
}
Oh btw, the init method of the DataStore looks like this, maybe thats of some help.
- (id) init
{
//If we allready have a singleton object
if(defaultStore){
return defaultStore;
}
self = [super init];
// Read in our .xcdatamodel file
model = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
NSPersistentStoreCoordinator *psc =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSString *path = pathInDocumentDirectory(@"store.data");
NSURL *storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]) {
[NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]];
}
// Create the managed object context
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
[context setUndoManager:nil];
[psc release];
return self;
}
Upvotes: 1
Views: 248
Reputation: 5817
One option would be to have a designated init that takes a filename, and then have the default initializer pass in the normal file name. Then your tests could create a DataStore in a separate file, deleting it before running the tests and after, without touching your non-test data.
Upvotes: 2