RxKotlin
RxKotlin copied to clipboard
Assertions for side-effects
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)
}
}
It's just a general idea. It could be polished to avoid gotchas such as awaitTerminalEvent()
never returning.