I need to write unit case for realm.io
migration. How do i simulate a before and after Object Class
?
Asked
Active
Viewed 895 times
3

perwyl
- 323
- 3
- 14
1 Answers
3
One solution is to have a realm file previously saved (such as in your unit test bundle) with the schema version from which you want to test the migration. Your realm object in code is kept in its most recent version, while the realm schema in disk contains an older version.
Have a look at the migration sample app.
In the sample there are versions V0
, V1
and V2
of the same Person
object, as well as three different realm database files with different schema versions default-v0.realm
, default-v1.realm
and default-v2.realm
.
The same migrationBlock
is then used in all versions of the database to ensure it can correctly bring any possible old schema to the newest V2
.
-
Thanks! How could i miss that! – perwyl Oct 28 '15 at 05:59
-
But doesn't opening the realm file and running the migration change the file on disk automatically? How would you run the unit test a second time? – Daniel Oct 29 '15 at 23:16
-
Make a copy of the database file and use that for running the tests. – Edman Oct 30 '15 at 04:11