I am running unit testing for a model file which uses core data. The test target builds successfully, but i am seeing the following errors :
error: testThatDistinguishedFolderTypeForTheAccountExists (FolderDALTests) failed: +entityForName: could not locate an entity named ‘Folder’ in this model.
error: testThatFindByExchageAccountReturnsFolders (FolderDALTests) failed: +entityForName: could not locate an entity named ‘ExchangeAccount’ in this model.
However, i feel that the problem is caused due to the “setup” method, where the NSManagedObjectModel is not created using merging all the models found in bundles.
-(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");
}
And the other method is :
- (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");
}
The setup method is :
- (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;
}
Here, “Folder” and “ExchangeAccount” are the core data entities. And their corresponding DAL(Data Access Layer) files have some of their business logic
I had forgotten to add the unit test as a target to the schema.