使用RxAndroid用例在演示者中测试什么以及如何测试
我的项目使用 我使用:使用RxAndroid用例在演示者中测试什么以及如何测试,android,unit-testing,dagger-2,clean-architecture,Android,Unit Testing,Dagger 2,Clean Architecture,我的项目使用 我使用: 表示层的MVP 使用RxAndroid并获得可处置观察者的用例 第二把匕首 演示者(Kotlin)的示例代码: fun doSomething(){ 执行(对象:DisposableObserver()){ 覆盖有趣的onStart(){ view.showLoadingIndicator()视图 } 覆盖onNext(数据列表:列表){ view.showDataItems(数据列表) } 覆盖有趣的错误(e:可丢弃){ view.downRorDialog() }
- 表示层的MVP
- 使用RxAndroid并获得可处置观察者的用例
- 第二把匕首
fun doSomething(){
执行(对象:DisposableObserver()){
覆盖有趣的onStart(){
view.showLoadingIndicator()视图
}
覆盖onNext(数据列表:列表){
view.showDataItems(数据列表)
}
覆盖有趣的错误(e:可丢弃){
view.downRorDialog()
}
覆盖有趣的onComplete(){
view.hideLoadingIndicator()
}
})
}
我想为此演示者编写单元测试
我的问题是:DisposableObserver中的不同方法调用是否值得测试(onStart、onNext…)?
如果是的话,看起来我需要将DisposableObserver注入演示者(以便我能够模拟它)。有没有更干净的方法?最终我找到了这个解决方案:
- 用于模拟对象和响应(如上所述)
- 用于能够使用在Kotlin中使用时不显示编译时错误的any()方法
- 当调用用例的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()
}
@测试
有趣的StopSloadingstateoComplete(){
//给定
给定(getInterestingDataUseCase.execute(any()))。
将{调用->
val observer=invocation.arguments[0]作为DisposableObserver
observer.onComplete()
}
//什么时候
myPreseneter.onReady()
//然后
然后(视图).should().hideLoadingIndicator()
}
TestObserver有帮助吗?谢谢,但这不是我想要的。我找到了一个解决方案并回答了我自己的问题是的,您仍然需要使用mockito进行模拟,并使用TestObserver进行测试
@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()
}