Android测试主持人MVP

时间:2018-10-20 20:12:38

标签: android unit-testing

我有演示者来获取数据用户。

class ListUserPresenterImpl @Inject constructor(val view: ListUserView,
                                            val context: Context,
                                            private val rxSchedulers: RxSchedulers,
                                            private val interactor: ListUserInteractor) : ListUserPresenter {

private val compositeDisposable = CompositeDisposable()

override fun loadUser(name: String) {
    view.showProgress(true)
    compositeDisposable.add(getThreadData(name).subscribe(::handleSuccess, ::handleError))
}

private fun getThreadData(name: String): Single<List<DataUserItem>> {
    compositeDisposable.clear()
    return interactor.getListUser(name)
            .subscribeOn(rxSchedulers.getSchedulersIO())
            .observeOn(rxSchedulers.getSchedulersMainThread())
            .doFinally { view.showProgress(false) }
}

private fun handleSuccess(list: List<DataUserItem>) {
    view.showListUser(list)
}

private fun handleError(throwable: Throwable) {
    view.showError()
}

}

我想编写一个测试以检查调用view.showListUser(list)。我尝试了一些选项,但是它们没有用。我该怎么办?

这是我的测试

@Before
fun initPresenter() {
    view = Mockito.mock(ListUserView::class.java)
    val interactor = ListUserInteractorImpl(repository = ListUserRepositoryImpl(FakeGitApi(), DataUserItemMapper()))
    presenter = ListUserPresenterImpl(view, Mockito.mock(Context::class.java), TestRxSchedulers(), interactor)
}

@Test
fun testLoadUser() {
    presenter.loadUser(USER_NAME)
    verify(view).showError()
}

测试完成下一个结果

希望但未调用: view.showError(); ->位于ru.portfolio.GitClient.list_user.TestListUserPresenter.testLoadUser(TestListUserPresenter.kt:41) 但是,与此模拟程序之间仅有1种交互: view.showProgress(true); ->在ru.portfolio.GitClient.presentation.list_user.presenter.ListUserPresenterImpl.loadUser(ListUserPresenterImpl.kt:37)

希望但未调用: view.showError(); ->位于ru.portfolio.GitClient.list_user.TestListUserPresenter.testLoadUser(TestListUserPresenter.kt:41) 但是,与此模拟程序之间仅有1种交互: view.showProgress(true); ->在ru.portfolio.GitClient.presentation.list_user.presenter.ListUserPresenterImpl.loadUser(ListUserPresenterImpl.kt:37)

0 个答案:

没有答案