Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
    }
}
like image 762
AkademiksQc Avatar asked Oct 18 '22 04:10

AkademiksQc


1 Answers

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)
    }
}
like image 68
kishikawa katsumi Avatar answered Oct 21 '22 02:10

kishikawa katsumi