我有以下unitTest:
@Test
fun loadMovieTrailer() {
val video = Video("id", "name", "site",
"videoId", 20, "type")
val observableResponse =
Observable.just(MovieApi.VideoWrapper(Lists.newArrayList(video)))
`when`(movieApi.trailers(anyString())).thenReturn(observableResponse)
with(viewModel) {
assertFalse(isTrailersVisible.get())
showTrailers(movie)
assertTrue(isTrailersVisible.get())
assertFalse(trailers.isEmpty())
assertTrue(trailers.size == 1)
}
}
这是movieApi.trailers
改造api方法:
@GET("3/movie/{movieId}/videos")
fun trailers(@Path("movieId") movieId: String): Observable<VideoWrapper>
我在MovieApi中有另一种方法,我想为它编写测试:
@GET("3/discover/movie?language=en&sort_by=popularity.desc")
fun popularMovies(@Query("page") page: Int): Call<MovieWrapper>
如您所见,它返回Call<T>
对象而不是Observable
。如何替换测试方法中的以下部分?
Observable.just(MovieApi.VideoWrapper(Lists.newArrayList(video)))
`when`(movieApi.trailers(anyString())).thenReturn(observableResponse)
中找到
答案 0 :(得分:0)
您可以为此使用retrofit-mock
。
val mockCall = Calls.response(Response.success(myTestData))
`when`(api.myFunction()).thenReturn(mockCall)