Last active
April 16, 2018 11:50
-
-
Save kazukinr/dca176479ac1ff3c56168b29cd499024 to your computer and use it in GitHub Desktop.
Mock of RealmResults example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.kazukinr.sample.realm.data | |
| import com.nhaarman.mockito_kotlin.any | |
| import com.nhaarman.mockito_kotlin.mock | |
| import com.nhaarman.mockito_kotlin.whenever | |
| import io.reactivex.Flowable | |
| import io.realm.RealmObject | |
| import io.realm.RealmResults | |
| object RealmTestHelper { | |
| inline fun <reified E : RealmObject> mockResults(data: List<E>): RealmResults<E> { | |
| val realmResults = mock<RealmResults<E>>() | |
| whenever(realmResults.isEmpty()).thenReturn(data.isEmpty()) | |
| whenever(realmResults.isLoaded()).thenReturn(true) | |
| whenever(realmResults.isValid()).thenReturn(true) | |
| whenever(realmResults.isManaged()).thenReturn(true) | |
| whenever(realmResults.size).thenReturn(data.size) | |
| whenever(realmResults.first()).thenReturn(data[0]) | |
| whenever(realmResults.first(any<E>())).thenAnswer { invocation -> | |
| if (data.isNotEmpty()) { | |
| data[0] | |
| } else { | |
| invocation.arguments[0] | |
| } | |
| } | |
| whenever(realmResults.get(any<Int>())).thenAnswer { invocation -> | |
| val index = invocation.arguments[0] as Int | |
| data[index] | |
| } | |
| whenever(realmResults.iterator()).thenReturn(data.toMutableList().iterator()) | |
| whenever(realmResults.asFlowable()).thenReturn(Flowable.fromCallable { | |
| realmResults | |
| }) | |
| return realmResults | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment