DIfficulty嘲笑核心数据对象

时间:2012-09-25 17:59:04

标签: unit-testing ios5

我正在运行使用核心数据的模型文件的单元测试。测试目标已成功构建,但我看到以下错误:

错误:testThatDistinguishedFolderTypeForTheAccountExists(FolderDALTests)失败:+ entityForName:无法在此模型中找到名为“Folder”的实体。

错误:testThatFindByExchageAccountReturnsFolders(FolderDALTests)失败:+ entityForName:无法在此模型中找到名为“ExchangeAccount”的实体。

但是,我觉得问题是由于“setup”方法造成的,其中NSManagedObjectModel不是使用合并捆绑包中找到的所有模型创建的。

-(void)testThatDistinguishedFolderTypeForTheAccountExists
 {
   Folder *inbox = [NSEntityDescription insertNewObjectForEntityForName:@"Folder" 
   inManagedObjectContext:self.context];
   inbox.distinguishedFolderType = @"inbox";
   ExchangeAccount *account = [NSEntityDescription insertNewObjectForEntityForName:@"ExchangeAccount" inManagedObjectContext:self.context];
   id<FolderProtocol> folder = [FolderDAL findWithAccount:account DistinguishedFolderType:@"inbox" withManagedContext:self.context];

STAssertTrue([folder.folderDistinguishedFolderType isEqualToString:@"inbox"], @"Unable to find the folder with the given distinguished type");

}

另一种方法是:

- (void)testThatFindByExchageAccountReturnsFolders
  {
      ExchangeAccount *account = [NSEntityDescription          insertNewObjectForEntityForName:@"ExchangeAccount" inManagedObjectContext:self.context];

     Folder *inbox = [NSEntityDescription insertNewObjectForEntityForName:@"Folder"     inManagedObjectContext:self.context];
     inbox.displayName = @"Inbox";
     inbox.exchangeAccount = account;

     Folder *calendars = [NSEntityDescription insertNewObjectForEntityForName:@"Folder" inManagedObjectContext:self.context];
     calendars.displayName = @"Calendars";
     calendars.exchangeAccount = account;

     NSArray *folders = [FolderDAL findByExchangeAccount:account withContext:self.context];

     STAssertTrue([folders count] > 0, @"No folders were returned");
  }

设置方法是:

- (void)setUp
{
  [super setUp];

  NSManagedObjectModel *objectModel = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]]];
  NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel];
  STAssertTrue([storeCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:NULL] ? YES : NO, @"Should be able to add in-memory store");
  self.context = [[NSManagedObjectContext alloc] init];
  self.context.persistentStoreCoordinator = storeCoordinator;

}

此处,“Folder”和“ExchangeAccount”是核心数据实体。它们相应的DAL(数据访问层)文件具有一些业务逻辑

1 个答案:

答案 0 :(得分:0)

我忘了将单元测试添加为架构的目标。