现在我正在使用 Core Data,如何对我的模型进行单元测试? [英] How to unit test my models now that I am using Core Data?
问题描述
我一直在使用域模型开发 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 扩展并使用 @dynamic 作为 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
中构建一个 Core Data 堆栈,然后将其拆除.使用 NSInMemoryPersistentStore
将使您的单元测试保持快速和内存中.将 @property (nonatomic,retain) NSManagedObjectContext *moc
添加到您的 TestCase 子类.然后:
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");
}
假设您的实体名为 Person
.顺便说一下,您的方法版本中存在内存泄漏;患者应该在非核心数据版本中-release
(insertNewObjectForEntityForName:managedObjectContext:
返回一个自动发布的实例).
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).
这篇关于现在我正在使用 Core Data,如何对我的模型进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!