现在我正在使用Core Data,如何对我的模型进行单元测试?

时间:2009-12-04 21:37:48

标签: objective-c unit-testing core-data

我一直在使用域模型开发一个iphone应用程序,并且推迟了应用程序的持久性方面。核心数据看起来是一个非常好的解决方案,因为我已经有一个定义良好的模型,但我遇到了现有单元测试的障碍。

以下是我现在所拥有的简单示例:

- (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");
}  

一旦我的Patient对象从NSManagedObject扩展并使用@dynamic作为firstName和lastName属性,我怎样才能使这个工作?

还有其他人使用Core Data遇到过这种类型吗?感谢。

2 个答案:

答案 0 :(得分:84)

您需要在每个方法或-setUp中构建核心数据堆栈,然后将其拆除。使用NSInMemoryPersistentStore可以使单元测试保持快速和内存。在TestCase子类中添加@property (nonatomic,retain) NSManagedObjectContext *moc。然后:

- (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;
}

您的测试方法如下:

- (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");
}

假设您的实体名为Person。顺便提一下,你的方法版本中存在内存泄漏;患者应该在非核心数据版本中-release'(insertNewObjectForEntityForName:managedObjectContext:返回一个自动释放的实例)。

答案 1 :(得分:22)

我使用了Barry Wark的上述答案,但我必须做一些修改才能使它适用于当前项目Xcode 5,iOS 7。

该物业保持不变:

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

设置必须首先必须更改为不发布,其次是提供模型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;
}

以下是示例测试用例:

- (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");
}