如何正确地对领域迁移进行单元测试 [英] How to properly unit test Realm migration

查看:57
本文介绍了如何正确地对领域迁移进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里查看了该帖子如何对单元迁移进行单元测试并尝试执行他们所说的以下内容:

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:

存储旧的.realm文件,并为迁移之前/之后编写测试.

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

我的单元测试包中有我的v0.realm文件,并正在创建它的副本以供使用.问题在于,在最近的迁移中,我从应用程序中删除了一个Type,因此也从Realm中删除了Type,我想测试它是否不再存在.

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.

设置领域配置时,我应该可以应用一些objectTypes,但是该类不再存在于我的应用程序中,并且我不确定如何检查它.

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.

这是我当前的测试设置,我在这里尝试测试我的对象在Realm v0中是否存在.请注意,这是我第一次使用Realm进行迁移,实际上是我第一次使用Realm.

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)

推荐答案

在没有类的情况下,我无法确定访问实体的方法,因此我决定将文件保留在我的项目中,以便确保迁移通过单元测试正常工作.

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.

这篇关于如何正确地对领域迁移进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆