Skip to content

Instantly share code, notes, and snippets.

@spllr
Created December 18, 2011 12:11
Show Gist options
  • Save spllr/1493212 to your computer and use it in GitHub Desktop.
Save spllr/1493212 to your computer and use it in GitHub Desktop.
Unit Testing CoreData Example Code
#import <SenTestingKit/SenTestingKit.h>
#import <CoreData/CoreData.h>
@interface MyCoreDataTests : SenTestCase
{
NSManagedObjectModel *managedObjectModel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectContext *managedObjectContext;
}
- (void)setupCoreDataEnvironment;
- (NSArray *)allEntities;
@end
#import "MyCoreDataTests.h"
@implementation MyCoreDataTests
- (void)setupCoreDataEnvironment
{
// Destroy objects
persistentStoreCoordinator = nil;
managedObjectModel = nil;
managedObjectContext = nil;
// Setup Core Data
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:managedObjectModel];
NSError *error;
// Setup an in memory store
if(![persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error]) {
STFail(@"Could not create store coordinator: %@", [error localizedDescription]);
}
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
[managedObjectContext setUndoManager:nil];
}
- (NSArray *)allEntities
{
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Contact" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];
return results;
}
- (void)testCanCreateEntity
{
// Setup the CoreData environment
[self setupCoreDataEnvironment];
// Create an entity. Assume we have defined an Entity called Contact
NSEntityDescription *createdContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact"
inManagedObjectContext: managedObjectContext];
// We now should have a fresly created entity
STAssertNotNil(createdContact, @"should have created a new entity");
}
- (void)testAddedObject
{
// Setup the CoreData environment
[self setupCoreDataEnvironment];
// We start of with an empty store
int numberOfEntitiesBeforeCreatingOne = [[self allEntities] count];
STAssertEquals(numberOfEntitiesBeforeCreatingOne, 0, @"no entities should be present");
// Create an entity
[NSEntityDescription insertNewObjectForEntityForName:@"Contact"
inManagedObjectContext: managedObjectContext];
// The new entity has been added to the store
int numberOfEntitiesAfterCreatingOne = [[self allEntities] count];
STAssertEquals(numberOfEntitiesAfterCreatingOne, 1, @"one entity should be present");
}
@end
- (void)setupCoreDataEnvironment
{
// Destroy objects
persistentStoreCoordinator = nil;
managedObjectModel = nil;
managedObjectContext = nil;
// Setup Core Data
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:managedObjectModel];
NSError *error;
// Setup an in memory store
if(![persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error]) {
STFail(@"Could not create store coordinator: %@", [error localizedDescription]);
}
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
[managedObjectContext setUndoManager:nil];
}
- (NSArray *)allEntities
{
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Contact" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSArray *results = [managedObjectContext executeFetchRequest:request error:nil];
return results;
}
- (void)testAddedObject
{
// Setup the CoreData environment
[self setupCoreDataEnvironment];
// We start of with an empty store
int numberOfEntitiesBeforeCreatingOne = [[self allEntities] count];
STAssertEquals(numberOfEntitiesBeforeCreatingOne, 0, @"no entities should be present");
// Create an entity
[NSEntityDescription insertNewObjectForEntityForName:@"Contact"
inManagedObjectContext: managedObjectContext];
// The new entity has been added to the store
int numberOfEntitiesAfterCreatingOne = [[self allEntities] count];
STAssertEquals(numberOfEntitiesAfterCreatingOne, 1, @"one entity should be present");
}
- (void)testCanCreateEntity
{
// Setup the CoreData environment
[self setupCoreDataEnvironment];
// Create an entity. Assume we have defined an Entity called Contact
NSEntityDescription *createdContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact"
inManagedObjectContext: managedObjectContext];
// We now should have a fresly created entity
STAssertNotNil(createdContact, @"should have created a new entity");
}
#import <SenTestingKit/SenTestingKit.h>
#import <CoreData/CoreData.h>
@interface MyCoreDataTests : SenTestCase
{
NSManagedObjectModel *managedObjectModel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectContext *managedObjectContext;
}
@end
#import <SenTestingKit/SenTestingKit.h>
#import <CoreData/CoreData.h>
@interface MyCoreDataTests : SenTestCase
{
NSManagedObjectModel *managedObjectModel;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectContext *managedObjectContext;
}
- (void)setupCoreDataEnvironment;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment