Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to unit test NSCoding?
    text
    copied!<p>I have an iOS application with data persisted using NSCoding and more precisely NSKeyedArchiver. This application is already available on the App Store.</p> <p>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.</p> <p><strong>In my tests, I want to dynamically generate persisted data with old data model, launch migration and see if everything went well.</strong></p> <p>Currently, archiving an object looks like this :</p> <pre><code>MyDataModelObject *object = .... NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:object forKey:key]; [archiver finishEncoding]; </code></pre> <p>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'.</p> <p><strong>Is there a way to simulate what is done in the <code>encodeWithCoder:</code> method of a class without using this class ?</strong></p> <hr> <p>What I would like to achieve is the following</p> <pre><code>- 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"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload