Writing unit tests against your model and code that uses Core Data is easy. For example, it's trivial to load your compiled model in a unit test:
Not only that, but you can introspect it:
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
And you can do this all the way down to the property level. This means that it's possible to assert that your entire model is set up the way you expect it to be. For example, you can make sure that your Employee entity has a mandatory salary attribute with a minimum value of 1 and a type of NSDecimalAttributeType, and descends from a Person entity that has a mandatory name attribute with a minimum length of 1 and a default value of "name."
NSArray *entities = [model entities];
But how do you test your use of Core Data? You just use Core Data in your tests as you would in your project. For example, to instantiate a complete Core Data "stack" (as it's sometimes referred to):
To instantiate managed objects associated with that context from entities in your model:
NSManagedObjectModel *model; NSPersistentStoreCoordinator *coordinator; NSManagedObjectContext *context; model = [[NSManagedObjectModel alloc] initWithContentsOfURL:urlToModel]; coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; context = [[NSManagedObjectContext alloc] init]; [context setPersistentStoreCoordinator:coordinator];
This gives you an autoreleased Employee (assuming your context's coordinator's model has an Employee entity, of course). It's that easy.NSManagedObject *employee; employee = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:context];
You can then do things like check that this object was created with the correct defaults (e.g. the ones specified in your model), that it posts KVO notifications properly for properties where you care about such things, and so on. You can even add a persistent store to the coordinator and test that saving and loading work (and don't work when they're supposed to fail, of course) just by using normal Core Data and Foundation APIs.
You can multiply the full power of data modeling with the full power of unit testing and test driven development. It kicks ass.