我想用 来模拟和测试我Presenter
的Observable
,但我不知道该怎么做,代码的主要部分如下:
//in my presenter:
override fun loadData(){
this.disposable?.dispose()
this.disposable =
Observable.create<List<Note>> {emitter->
this.notesRepository.getNotes {notes->
emitter.onNext(notes)
}
}
.doOnSubscribe {
this.view.showProgress()
}
.subscribe {
this.view.hideProgress()
this.view.displayNotes(it)
}
}
//in test:
@Test
fun load_notes_from_repository_and_display(){
val loadCallback = slot<(List<Note>)->Unit>();
every {
notesRepository.getNotes(capture(loadCallback))
} answers {
//Observable.just(FAKE_DATA)
loadCallback.invoke(FAKE_DATA)
}
notesListPresenter.loadData()
verifySequence {
notesListView.showProgress()
notesListView.hideProgress()
notesListView.displayNotes(FAKE_DATA)
}
}
我得到了错误:
Verification failed: call 2 of 3: IView(#2).hideProgress()) was not called.
那么,如何在 Android 单元测试中使用 Mockk 测试 Rx 的东西呢?提前致谢!