如何模拟返回Observable的被动库

所以我有知识库,它提供了客户端的Observable。 有没有办法我可以嘲笑这个存储库,所以我不需要从我的模拟器发送位置或使用真实的设备获得一些位置?

接口如下所示:

interface RxLocationRepository { @SuppressLint("MissingPermission") fun onLocationUpdate(): Observable<Location> fun stopLocationUpdates() } 

在我的客户端,我使用这样的:

 class LocationManager( val rxLocationRepository: RxLocationRepository){ private fun appendGeoEvent(location: Location) { val locationGeoEvent = LocationGeoEvent( accuracy = location.accuracy.toDouble(), latitude = location.latitude, longitude = location.longitude, timestampGeoEvent = location.time ) processGeoEvent(locationGeoEvent) } compositeDisposable.add(rxLocationRepository.onLocationUpdate() .subscribe(Consumer { location -> appendGeoEvent(location) })) .... 

所以我发送这个获得的位置我的appendGeoEvent方法。

我可以使用例如Mockito,但我不知道如何嘲笑这个存储库,所以我可以使用假的位置。 另外,我想用Kotlin。

如果使用Mockito,你可以做这样的事情:

 import android.location.Location import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito import org.mockito.Mockito.`when` import org.mockito.Mockito.mock import org.mockito.junit.MockitoJUnitRunner import java.util.* @RunWith(MockitoJUnitRunner::class) class LocationsTest{ @Test fun should_return_one_location() { val location = Location("test").apply { latitude = 1.234 longitude = 5.678 // ... } val mockRepository = mock(RxLocationRepository::class.java) `when`(mockRepository.onLocationUpdate()).thenReturn(Observable.just(location)) // use the mock } } 

我正在使用: testCompile "org.mockito:mockito-core:2.11.0"