如何单元测试我的模型现在,我使用核心数据? [英] How to unit test my models now that I am using Core Data?

查看:146
本文介绍了如何单元测试我的模型现在,我使用核心数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果我的Patient对象从NSManagedObject扩展并使用@动态的firstName和lastName属性?

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

有没有人在Core Data中运行这种类型?感谢。

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

推荐答案

您需要在每个方法或 -setUp ,然后将其撕下。使用 NSInMemoryPersistentStore 将保持快速和内存中的单元测试。向您的TestCase子类添加 @property(nonatomic,retain)NSManagedObjectContext * moc 。然后:

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

您的测试方法如下:

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


$ c>。在你的版本的方法中有一个内存泄漏,顺便说一下;病人应该在非核心数据版本( insertNewObjectForEntityForName:managedObjectContext:返回一个自动释放的实例) -release

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).

这篇关于如何单元测试我的模型现在,我使用核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆