我正在使用自定義實體遷移策略爲遷移創建映射模型,並且我真的想爲此遷移構建一些單元測試。當我運行應用程序時,遷移似乎正常工作,但是當我通過單元測試運行遷移時,我的NSEntityMigrationPolicy子類方法根本不會被調用。我如何測試核心數據遷移?
我正在使用Xcode的內置OCUnit框架。
我的測試代碼:
- (void)test1to2Migration_appIdentifierMoved {
[self createVersion1Store];
// TODO Perform migration
NSManagedObjectModel *version1Model = [self version1Model];
NSManagedObjectModel *version2Model = [self version2Model];
NSError *error = nil;
NSMappingModel *mappingModel = [NSMappingModel
inferredMappingModelForSourceModel:version1Model
destinationModel:version2Model error:&error];
STAssertNotNil(mappingModel, @"Error finding mapping model: %@", error);
NSMigrationManager *migrationManager =
[[[NSMigrationManager alloc]
initWithSourceModel:version1Model
destinationModel:version2Model]
autorelease];
BOOL migrationSucceeded =
[migrationManager migrateStoreFromURL:self.version1StoreURL
type:NSSQLiteStoreType
options:nil
withMappingModel:mappingModel
toDestinationURL:self.version2StoreURL
destinationType:NSSQLiteStoreType
destinationOptions:nil
error:&error];
STAssertTrue(migrationSucceeded, @"Error migrating store: %@", error);
// TODO Verify appIdentifier is moved from Project to its Tests
[self deleteTempStores];
}
我的映射模型指定用於定義-createRelationshipsForDestinationInstance:entityMapping:manager:error:
方法定製NSEntityMigrationPolicy,但我的政策是永遠不會從單元測試調用。當我運行遷移時,模型被修改爲新版本 - 預期的屬性顯示在正確的位置。
任何想法如何讓我的遷移政策在單元測試中工作?