我正在对一个使用核心数据的模型文件运行单元测试。测试目标构建成功,但我看到以下错误:
错误:(FolderDALTests)失败:+entityForName:在此模型中找不到名为'Folder‘的实体。
错误: testThatFindByExchageAccountReturnsFolders (FolderDALTests)失败:+entityForName:在此模型中找不到名为'ExchangeAccount‘的实体。
然而,我觉得这个问题是由"setup“方法引起的,在”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(数据访问层)文件有它们的一些业务逻辑
发布于 2012-10-12 14:05:21
我忘了将单元测试作为目标添加到模式中。
https://stackoverflow.com/questions/12588544
复制相似问题