56

I have been developing an iphone application using a domain model, and have put off the persistence aspect of the app until now. Core Data looks like a really good solution since I already have a well defined model but I am running into a snag with my existing unit tests.

Here is simple example of what I have now:

- (void)test_full_name_returns_correct_string {
    Patient *patient = [[Patient alloc] init];  
    patient.firstName = @"charlie";
    patient.lastName = @"chaplin";
    STAssertTrue([[patient fullName] isEqualToString:@"charlie chaplin"], @"should have matched full name");
}  

How can I make this work once my Patient object extends from NSManagedObject and uses @dynamic for the firstName and lastName properties?

Has anyone else run into this type of this with Core Data? Thanks.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Tony Eichelberger
  • 7,044
  • 7
  • 37
  • 46

2 Answers2

85

You need to build a Core Data stack, either within each method or in -setUp and then tear it down. Using an NSInMemoryPersistentStore will keep things fast and in-memory for your unit tests. Add a @property (nonatomic,retain) NSManagedObjectContext *moc to your TestCase subclass. Then:

- (void)setUp {
  NSManagedObjectModel *mom = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:bundleContainingXCDataModel]];
  NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
  STAssertTrue([psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:NULL] ? YES : NO, @"Should be able to add in-memory store");    
  self.moc = [[NSManagedObjectContext alloc] init];
  self.moc.persistentStoreCoordinator = psc;

  [mom release];
  [psc release];

}

- (void)tearDown {
  self.moc = nil;
}

Your test method then looks like:

- (void)test_full_name_returns_correct_string {
    Patient *patient = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.moc];

    patient.firstName = @"charlie";
    patient.lastName = @"chaplin";
    STAssertTrue([[patient fullName] isEqualToString:@"charlie chaplin"], @"should have matched full name");
}

assuming your entity is named Person. There was a memory leak in your version of the method, by the way; patient should be -release'd in the non-Core Data version (insertNewObjectForEntityForName:managedObjectContext: returns an autoreleased instance).

prairiedogg
  • 6,323
  • 8
  • 44
  • 52
Barry Wark
  • 107,306
  • 24
  • 181
  • 206
  • Thanks for the help. I will go this route. About the memory leak, I haven't bothered with cleaning up memory in my unit tests. It seems more readable to me without the releases. Is there a benefit for keeping the tests from leaking? – Tony Eichelberger Dec 04 '09 at 22:40
  • 2
    If your tests leak, it's really hard to use your unit test suite to test your *other* code for leaks. Instruments has a leak analyzer that's basically useless if you obscure *reall* leaks with unnecessary (but intentional) leaks of the same classes in your test code. – Barry Wark Dec 04 '09 at 22:48
  • 3
    I think you'll find that the retain/release code disappears from conscious vision after a while. I rarely notice it anymore--unless it's missing. – Barry Wark Dec 04 '09 at 22:49
  • 1
    I am receiving a warning with `[mom release]` and `[pcs release]` which says `'release' is unavailable: not available in automatic reference counting mode`. How do I get around that issue? – BrightIntelDusk Apr 03 '14 at 22:09
  • 2
    You can remove the release calls.. those are only needed if your in a non-arc project. – Tony Apr 22 '14 at 16:31
  • Thanks for this explanation. Helped me a lot to get underway. Did found out though that unit testing apparently does instantiate your AppDelegate. In my case, I have my App's moc setup in the app delegate. So as advice for posterity: tests will run into issues if the code you're testing creates new ManagedObjects on the 'main' moc from your to-be-tested code. In my case, I resorted to changing test setup to fetch moc from AppDelegate, instead of creating a test-only moc. – wintvelt Nov 05 '15 at 12:00
22

I used the answer above by Barry Wark, but I had to do some modifications to make it work with the current projects Xcode 5, iOS 7.

The property stayed the same:

@interface SIDataTest : XCTestCase
    @property (nonatomic, retain) NSManagedObjectContext *moc;
@end

The setup had to actually had to change first of all to not release and secondly to provide a model URL.

- (void)setUp
{
    [super setUp];
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SimpleInvoice" withExtension:@"momd"];
    NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
    XCTAssertTrue([psc addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:NULL] ? YES : NO, @"Should be able to add in-memory store");
    self.moc = [[NSManagedObjectContext alloc] init];
    self.moc.persistentStoreCoordinator = psc;
}

Here is the example test case:

- (void)testCreateNew
{
    Invoice *newInvoice = [NSEntityDescription insertNewObjectForEntityForName:@"Invoice" inManagedObjectContext:self.moc];
    newInvoice.dueDate = [NSDate date];
    NSString* title = [[NSString alloc] initWithFormat:@"Invoice %@", @112];
    newInvoice.title = title;

    // Save the context.
    NSError *error = nil;
    if (![self.moc save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        XCTFail(@"Error saving in \"%s\" : %@, %@", __PRETTY_FUNCTION__, error, [error userInfo]);
    }
    XCTAssertFalse(self.moc.hasChanges,"All the changes should be saved");
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
mikebz
  • 3,277
  • 8
  • 37
  • 50