RxKotlin icon indicating copy to clipboard operation
RxKotlin copied to clipboard

Assertions for side-effects

Open pakoito opened this issue 5 years ago • 1 comments

Based off this tweet: https://twitter.com/pacoworks/status/1098377239786221568

fun <T> testSideEffect(f: TestObserver<T>.((T)-> Unit) -> Unit): Unit {
  val obs = TestObserver<T>()
  val sub = PublishSubject.create<T>().apply { subscribe(obs) }
  f(obs) { sub.onNext(it) }
}

fun main() {
    testSideEffect<Int> { consumer ->
        Observable.just(1).doOnNext { consumer(it) }.subscribe()

        assertValueCount(1)
        assertValues(10)
    }
}

pakoito avatar Mar 11 '19 02:03 pakoito

It's just a general idea. It could be polished to avoid gotchas such as awaitTerminalEvent() never returning.

pakoito avatar Mar 11 '19 02:03 pakoito