【发布时间】:2020-02-19 10:15:15
【问题描述】:
我正在尝试使用改造和 rxJava 测试我的服务器调用。我在 koin 中使用 MVP 模式,当我尝试测试执行调用以从服务器获取数据的方法时遇到了一些问题。
我有一个调用交互器来检索数据的演示器。 Interactor DI 是用 koin 完成的。
我在这里和谷歌做了一些研究,我一直在看的所有例子都不适合我。
我遇到的错误是这样的:
Wanted but not invoked:
callback.onResponseSearchFilm(
[Film(uid=1, id=1724, title=The incredible Hulk, tagline=You'll like him when he's angry., overview=Scientist Bruce Banner scours the planet for an antidote to the unbridled force of rage within..., popularity=22.619048, rating=6.1, ratingCount=4283, runtime=114, releaseDate=2008-06-12, revenue=163712074, budget=150000000, posterPath=/bleR2qj9UluYl7x0Js7VXuLhV3s.jpg, originalLanguage=en, genres=null, cast=null, poster=null, favourite=false), Film(uid=2, id=1724, title=The incredible Hulk, tagline=You'll like him when he's angry., overview=Scientist Bruce Banner scours the planet for an antidote to the unbridled force of rage within..., popularity=22.619048, rating=8.0, ratingCount=4283, runtime=114, releaseDate=2008-06-12, revenue=163712074, budget=150000000, posterPath=/bleR2qj9UluYl7x0Js7VXuLhV3s.jpg, originalLanguage=en, genres=null, cast=null, poster=null, favourite=false), Film(uid=3, id=1724, title=The incredible Hulk, tagline=You'll like him when he's angry., overview=Scientist Bruce Banner scours the planet for an antidote to the unbridled force of rage within..., popularity=22.619048, rating=8.5, ratingCount=4283, runtime=114, releaseDate=2008-06-12, revenue=163712074, budget=150000000, posterPath=/bleR2qj9UluYl7x0Js7VXuLhV3s.jpg, originalLanguage=en, genres=null, cast=null, poster=null, favourite=false)]
);
-> at com.filmfy.SearchImplTest.loadItems_WhenDataIsAvailable(SearchImplTest.kt:30)
Actually, there were zero interactions with this mock.
这是我的测试
class SearchImplTest: KoinTest {
private val searchImpl: SearchImpl = mock()
private val callback: SearchContract.Callback? = mock()
private val api: RetrofitAdapter = mock()
@Test
fun loadItems_WhenDataIsAvailable() {
`when`(api.getFilms()).thenReturn(Observable.just(filmRequestFacke()))
searchImpl.getfilms(callback)
verify(callback)?.onResponseSearchFilm(fackeFilms())
}
}
我的交互代码:
class SearchImpl : AbstractInteractor() {
private val voucherApiServe by lazy {
RetrofitAdapter.create()
}
fun getfilms(callback: SearchContract.Callback?){
disposable = voucherApiServe.getFilms()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result -> processFilmSearch(result.data, callback)},
{ error -> processError(error) }
)
}
fun processFilmSearch(filmList : ArrayList<Film>?, callback: SearchContract.Callback?){
callback?.onResponseSearchFilm(filmList)
}
.
.
.
我的 koin 模块:
factory<SearchContract.Presenter> { (view: SearchContract.View) -> SearchPresenter(view, mSearchImpl = get()) }
API调用
@GET(Api.ENDPOINT.FILMS)
fun getFilms(): Observable<FilmRequest>
【问题讨论】:
标签: android unit-testing rx-java retrofit2