Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to build a Core Data stack, either within each method or in <code>-setUp</code> and then tear it down. Using an <code>NSInMemoryPersistentStore</code> will keep things fast and in-memory for your unit tests. Add a <code>@property (nonatomic,retain) NSManagedObjectContext *moc</code> to your TestCase subclass. Then:</p> <pre><code>- (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; } </code></pre> <p>Your test method then looks like:</p> <pre><code>- (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"); } </code></pre> <p>assuming your entity is named <code>Person</code>. There was a memory leak in your version of the method, by the way; patient should be <code>-release</code>'d in the non-Core Data version (<code>insertNewObjectForEntityForName:managedObjectContext:</code> returns an autoreleased instance).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload