RXpect extends Mockito to simplify unit testing for RX Kotlin applications. It takes care of synchronizing your test with your application code.
class MyPresenter(private val view: MyView) {
fun onResume() {
disposable = users.getUsers().subscribeBy {
onSuccess = { view.show(it)}
onError = { println("ERROR: $it") }
}
}
}
@Test
fun userIsShown() {
whenever(users.getUsers()).thenReturn(Single.just(user))
val expectation = expect(view.show(user))
presenter.onResume()
expectation.verify()
}
class MyPresenter {
fun onResume() {
users.getUsers().subscribeBy {
onSuccess = ::println
onError = { println("ERROR: $it") }
}
}
}
@Test
fun showsUser() {
val expectation = expectSubscribe(users.getUsers()).thenEmit(user)
presenter.onResume()
expectation.verify()
}
class MyPresenter(private val users: Users) {
private var disposable: Disposable? = null
fun onResume() {
disposable = users.getUsers().subscribeBy {
onSuccess = ::println
onError = { println("ERROR: $it") }
}
}
fun onPause() {
disposable?.dispose()
}
}
@Test
fun loadingUsersDisposedOnPause() {
val expectation = expectDispose(users.getUsers())
presenter.onResume()
presenter.onPause()
expectation.verify()
}