我正在为使用核心数据的模型文件运行单元测试。测试目标构建成功,但我看到以下错误:
错误:testThatDistinguishedFolderTypeForTheAccountExists (FolderDALTests) 失败:+entityForName:无法在此模型中找到名为“文件夹”的实体。
错误:testThatFindByExchageAccountReturnsFolders (FolderDALTests) 失败:+entityForName:无法在此模型中找到名为“ExchangeAccount”的实体。
但是,我觉得问题是由于“设置”方法引起的,其中 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(Data Access Layer)文件也有一些业务逻辑