Created
December 18, 2011 12:11
-
-
Save spllr/1493212 to your computer and use it in GitHub Desktop.
Unit Testing CoreData Example Code
This file contains 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 MyCoreDataTests : SenTestCase | |
{ | |
NSManagedObjectModel *managedObjectModel; | |
NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
NSManagedObjectContext *managedObjectContext; | |
} | |
- (void)setupCoreDataEnvironment; | |
- (NSArray *)allEntities; | |
@end |
This file contains 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 "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 |
This file contains 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
- (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]; | |
} |
This file contains 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
- (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"); | |
} |
This file contains 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
- (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"); | |
} |
This file contains 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 MyCoreDataTests : SenTestCase | |
{ | |
NSManagedObjectModel *managedObjectModel; | |
NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
NSManagedObjectContext *managedObjectContext; | |
} | |
@end |
This file contains 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 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