How to fake real-time results for tests

I wrote a test to check if a function was called:

func test_getTaskLists_doNotCreateOrUpdateTaskListToStorageWhenSynchedLocally() { ... let (datasource, restAPI, fakeTaskListStorage) = ... datasource.getTaskLists() { (taskLists, error) -> Void in ... XCTAssertEqual(1, fakeTaskListStorage.readAllInvocationCount) ... } ... } 

The function is tricked to bypass the super implementation, and the problem is that the function returns results that I cannot understand, to build / mock, to return a valid object, so that the compiler stops complaining ... I know that I can 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 this is possible

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

Unable to instantiate Results directly. The Results subclass does not allow either. I think the best way to hide Results by protocol, such as ResultsWrapper , and not directly using Results .

But a lightweight workaround uses the memory area in memory when testing. FakeTaskListsStorageRealm readAll() can be written using a memory region in memory 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) } } 
+3
source

All Articles