How to fake Realm Results for tests

冷暖自知 提交于 2019-12-05 08:47:49

There is no way to instantiate Results directly. Subclassing Results doesn't allow too. I think the best way is hiding Results by protocol like ResultsWrapper rather than using Results directly.

But an easy workaround is using in-memory Realm when testing. The FakeTaskListsStorageRealm's readAll() can be written using in-memory Realm as follows:

class FakeTaskListsStorageRealm : TaskListStorageRealm {
    var fakeTaskLists:[TaskList]?
    override func readAll() -> RealmSwift.Results<TaskList> {
        readAllInvocationCount += 1
        return try! Realm(configuration: Realm.Configuration(inMemoryIdentifier: "test")).objects(TaskList.self)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!