1

Is there a simple way out there to create NSManagedObjects for testing reasons without to use the managed object context created for the release Application?

I'm now into Core Data coding for a couple of weeks but still having some issues in the details ... why can't I just alloc and init objects for testing? Do I really have to handle with a second persistant store / managed object context (and which one)?

I have to test some methods written in my NSManagedObject subclasses ...

CGee
  • 1,650
  • 5
  • 20
  • 31

1 Answers1

4

Trust me, you do NOT want to test core data objects without using a MOC. You have to do unsound things at best.

However, if you don't want to use your actual database, use an in memory store. It's very simple to set up. In fact, it's what I use for a lot of my own unit testing.

I caution you, though. There are a number of things that do not behave the same with SQL stores and in-memory stores. The most common problem will be with predicates. Read the docs to make sure your predicates are right.

I will say that during testing, you can use the in-memory MOC, but you should have a configuration that runs ALL you tests on the actual database itself to make sure it al works. For speed, maybe you use the in-memory database for normal use, and use the actual one for scheduled continuous-integration testing.

As an example, you can do something like this to create your in-memory MOC...

- (NSManagedObjectContext*)managedObjectContextWithConcurrencyType:(NSManagedObjectContextConcurrencyType)concurrencyType {
    NSManagedObjectModel *mom = [NSManagedObjectModel mergedModelFromBundles:nil];
    STAssertNotNil(mom, @"Can not create MOM from main bundle");

    NSPersistentStoreCoordinator *psc = [[MyPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
    STAssertNotNil(psc, @"Can not create persistent store coordinator");
    NSPersistentStore *store = [psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:0];
    STAssertNotNil(store, @"Can not create In-Memory persistent store");

    MyManagedObjectContext *moc = [[MyManagedObjectContext alloc] initWithConcurrencyType:concurrencyType];
    moc.persistentStoreCoordinator = psc;

    return moc;
}
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • Thanks, I'll try this! But I have a question: where would you place the example code? In every single test class? What's the best practice of doing this? – CGee Jul 03 '12 at 08:05
  • And one more question: why do you subclass NSPersistantStoreCoordinator and NSManagedObjectContext there? Can I do it without subclassing? – CGee Jul 03 '12 at 08:11
  • And one more problem: I'm getting Apple Mach-O Linker Errors for all the Core Data classes even after putting "#import " into my .h file ... – CGee Jul 03 '12 at 08:35
  • I have a simple utility file that contains stuff for testing. The code just creates objects of the native types. There is no sub-classing involved at all. You have to add the CoreData framework to your project -- "Build Phases" --> "Link Binary with Libraries" and add CoreData – Jody Hagins Jul 04 '12 at 03:42