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

Ton*_*ger 56 unit-testing core-data objective-c

我一直在使用域模型开发一个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");
}  
Run Code Online (Sandbox Code Playgroud)

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

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

Bar*_*ark 85

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

您的测试方法如下:

- (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");
}
Run Code Online (Sandbox Code Playgroud)

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

  • 我想你会发现保留/释放代码在一段时间后从有意识的视觉中消失了.我很少注意到它 - 除非它丢失了. (3认同)
  • 如果您的测试泄漏,则很难使用您的单元测试套件来测试您的*其他*代码是否泄漏.如果您在测试代码中使用相同类别的不必要(但有意)泄漏来掩盖*reall*泄漏,那么仪器有一个基本无用的泄漏分析仪. (2认同)
  • 您可以删除释放调用..只有在非弧项目中才需要这些调用. (2认同)

mik*_*ebz 22

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

该物业保持不变:

@interface SIDataTest : XCTestCase
    @property (nonatomic, retain) NSManagedObjectContext *moc;
@end
Run Code Online (Sandbox Code Playgroud)

实际上,设置必须首先改变为不发布,其次是提供模型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;
}
Run Code Online (Sandbox Code Playgroud)

以下是示例测试用例:

- (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");
}
Run Code Online (Sandbox Code Playgroud)