在翻新中使用Call <t>对象时进行本地单元测试

时间:2019-01-05 11:46:31

标签: android unit-testing kotlin

我有以下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)

完整的示例代码可在https://github.com/Ali-Rezaei/TMDb-Paging

中找到

1 个答案:

答案 0 :(得分:0)

您可以为此使用retrofit-mock

val mockCall = Calls.response(Response.success(myTestData))
`when`(api.myFunction()).thenReturn(mockCall)
相关问题