AkademiksQc
AkademiksQc

Reputation: 698

How to fake Realm Results for tests

I have written a test to validate if a function is called :

  func test_getTaskLists_doNotCreateOrUpdateTaskListToStorageWhenSynchedLocally() {

   ...    
    let (datasource, restAPI, fakeTaskListStorage) = ...

    datasource.getTaskLists() { (taskLists, error) -> Void in
      ...
      XCTAssertEqual(1, fakeTaskListStorage.readAllInvocationCount)
      ...      
    }
...
  }

The function is mocked to bypass super implementation and the issue is that the function returns a Results which I can't figure out to build/mock in order to return a valid object so the compiler stops complaining...I know I could just call super.readAll() but here I actually want to convert my test data (fakeTaskLists) to a fake Result object so everyone is happy...not sure if thats possible

class FakeTaskListsStorageRealm : TaskListStorageRealm {
    var fakeTaskLists:[TaskList]?
    override func readAll() -> RealmSwift.Results<TaskList> {
      readAllInvocationCount += 1
      //Here I want to return fakeTaskLists somehow...
    }
}

Upvotes: 6

Views: 2081

Answers (1)

kishikawa katsumi
kishikawa katsumi

Reputation: 10573

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)
    }
}

Upvotes: 3

Related Questions