1

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)
sargturner
  • 540
  • 2
  • 18

1 Answers1

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.

sargturner
  • 540
  • 2
  • 18
  • 1
    In order for you to open a Realm with a type that's no longer declared in your project, you'd need to open a Realm in "dynamic" mode, but it appears that the way you've done it is sufficient and avoids using less known parts of the Realm APIs. – jpsim Jul 16 '17 at 19:15