I have an iOS application with data persisted using NSCoding and more precisely NSKeyedArchiver. This application is already available on the App Store.
I'm working on version 2 of the application and the data model should change. So I need to handle data model migration. I want it covered by unit tests.
In my tests, I want to dynamically generate persisted data with old data model, launch migration and see if everything went well.
Currently, archiving an object looks like this :
MyDataModelObject *object = ....
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:object forKey:key];
[archiver finishEncoding];
The problem is, MyDataModelObject might be re-factored or even deleted in the version 2 of the application. So I can't use this class in my tests to generate an 'old version archive'.
Is there a way to simulate what is done in the encodeWithCoder:
method of a class without using this class ?
What I would like to achieve is the following
- testMigrationFrom_v1_to_v2 {
// simulate an archive with v1 data model
// I want this part of the code to be as simple as possible
// I don't want to rely on old classes to generate the archive
NSDictionary *person = ... // { firstName: John, lastName: Doe }
NSDictionary *adress = ... // { street: 1 down street, city: Butterfly City }
[person setObject:adress forKey:@"adress"];
// there's something missing to tell the archiever that:
// - person is of type OldPersonDataModel
// - adress is of type OldAdressDataModel
[archiver encodeObject:person forKey:@"somePerson"];
// at this point, I would like the archive file to contain :
// a person object of type OldPersonDataModel, that has an adress object of type OldAdressModel
NewPersonModel *newPerson = [Migration readDataFromV1];
// assertions
NSAssert(newPerson.firstName, @"John");
NSAssert(newPerson.lastName, @"Doe");
}