How to properly unit test Realm migration

荒凉一梦 提交于 2019-12-08 04:28:07

问题


I have looked at the post here How to unit test Realm migrations and am trying to implement what they have stated which is the following:


Store old .realm files and write tests for before / after migrations.


I have my v0.realm file in my unit test bundle and am creating a copy of it to work with. The issue is that with my latest migration I removed a Type from my application and thus from Realm and I'd like to test that it no longer exists.

When I set up my realm configuration I should be able to apply some objectTypes, however the class is no longer in my application and I am unsure how to check for it.

Here's my current test set up where I am trying to test that my object exists in Realm v0. Note that this is my first migration with Realm and really my first experience with Realm.

let realmV0 = loadRealmFromResource(withName: "realm-v0")

// Test that MyEntity exists

// How can I specify my objectTypes without having access to MyEntity.self
// since it no longer exists in my project?
let configuration = Realm.Configuration(fileURL: realmV0, deleteRealmIfMigrationNeeded: true, objectTypes: objectTypes)
let realm = try! Realm(configuration: configuration)

let results = realm.dynamicObjects("MyEntity")

XCTAssert(results.count > 0)

回答1:


I wasn't able to determine a way to to access the entity without the class so I decided to keep the file in my project so I can ensure that migration is working properly via unit tests.

Although the class is not needed for migration since I can delete an entity using the class name in a string I felt safer about leaving a small file in my project than hoping migration worked correctly.



来源:https://stackoverflow.com/questions/45065823/how-to-properly-unit-test-realm-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!