Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 既然我正在使用核心数据,如何对我的模型进行单元测试?_Objective C_Unit Testing_Core Data - Fatal编程技术网

Objective c 既然我正在使用核心数据,如何对我的模型进行单元测试?

Objective c 既然我正在使用核心数据,如何对我的模型进行单元测试?,objective-c,unit-testing,core-data,Objective C,Unit Testing,Core Data,我一直在使用域模型开发一个iphone应用程序,并将应用程序的持久性方面推迟到现在。核心数据看起来是一个非常好的解决方案,因为我已经有了一个定义良好的模型,但我的现有单元测试遇到了障碍 以下是我现在拥有的简单示例: - (void)test_full_name_returns_correct_string { Patient *patient = [[Patient alloc] init]; patient.firstName = @"charlie"; patie

我一直在使用域模型开发一个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属性,我该如何实现这一点


还有其他人遇到过这种类型的核心数据吗?谢谢。

您需要在每个方法内或在
-setUp
中构建一个核心数据堆栈,然后将其拆下。使用
nsimemorypersistentstore
将使您的单元测试保持快速和内存。将
@属性(非原子,保留)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;
}
您的测试方法如下所示:

- (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
'd(
insertNewObjectForEntityForName:managedObjectContext:
返回一个自动释放的实例)。

我使用了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");
}

谢谢你的帮助。我要走这条路。关于内存泄漏,我没有在单元测试中清理内存。如果没有发行版,我觉得它更可读。防止测试泄漏有什么好处吗?如果您的测试泄漏,那么使用您的单元测试套件来测试其他代码是否泄漏确实很困难。Instruments有一个泄漏分析器,如果你在测试代码中用不必要的(但有意的)相同类的泄漏来掩盖reall泄漏,它基本上是无用的。我想你会发现保留/释放代码在一段时间后从有意识的视野中消失。我很少再注意到它了——除非它丢失了。我收到了一条关于
[mom release]
[pcs release]
的警告,上面说
'release'不可用:在自动参考计数模式下不可用。我该如何回避这个问题?您可以删除释放调用。。只有在非arc项目中才需要这些。