【问题标题】:What and how to test in a Presenter with a RxAndroid Use Case使用 RxAndroid 用例在 Presenter 中测试什么以及如何测试
【发布时间】:2017-11-02 19:48:21
【问题描述】:

我的项目使用clean architecture

我用:

  • 表示层的 MVP
  • 使用 RxAndroid 并获取 DisposableObservers 的用例
  • 用于 DI 的匕首 2

来自演示者的示例代码 (Kotlin):

fun doSomething() {

    getInterestingDataUseCase.execute(object : DisposableObserver<List<InterestingDataItem>>() {

        override fun onStart() {
            view.showLoadingIndicator()
        }

        override fun onNext(dataList: List<InterestingDataItem>) {
            view.showDataItems(dataList)
        }

        override fun onError(e: Throwable) {
            view.showErrorDialog()
        }

        override fun onComplete() {
            view.hideLoadingIndicator()
        }
    })
}

我想为这个演示者编写单元测试。

我的问题是: DisposableObserver 中的不同方法调用是否值得测试(onStart、onNext...)? 如果是的话,看起来我需要将 DisposableObserver 注入演示者(以便我能够模拟它)。有没有更清洁的方法?

【问题讨论】:

  • TestObserver infoq.com/articles/Testing-RxJava2 有帮助吗?
  • 谢谢,但这不是我想要的。我找到了解决方案并回答了我自己的问题
  • 是的,您仍然必须使用 mockito 进行模拟并使用 TestObserver 进行测试

标签: android unit-testing dagger-2 clean-architecture


【解决方案1】:

最终我得到了这个解决方案:

  • 使用Mockito 模拟对象和响应(如here 所述)
  • 使用 Mockito Kotlin 可以使用在 Kotlin 中使用时不会显示编译时错误的 any() 方法
  • 在调用 UseCase 的 execute 方法时模拟 DisposableObserver 的行为

在请求完成时检查视图是否隐藏其进度指示器的测试示例:

@Test
fun stopsLoadingStateOnComplete() {

    //given
    given(getInterestingDataUseCase.execute(any())).
            will { invocation ->
                val observer = invocation.arguments[0] as DisposableObserver<List<InterestingDataItem>>
                observer.onComplete()
            }

    //when
    myPreseneter.onReady()

    //then
    then(view).should().hideLoadingIndicator()
}

【讨论】:

    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多