While trying to test-drive a RxJava app in Kotlin, I ran into some problems. Here are my notes on that topic.
1
When using the RxJava TestSubscriber to test Observables, I have to make sure that I don’t use subscribeOn
or observeOn
(RxJava version 1.1.0
introduced many methods that make the TestSubscriber actually pretty useful).
val testSubscriber = TestSubscriber<ThingsIWant>()
DataSource().subscribeOn(Schedulers.test())
.subscribe(testSubscriber)
testSubscriber.assertValueCount(42)
Will not work even though the Schedulers.test()
looks like it’s made for… tests.
The documentation is not clear on that it simply says:
Creates and returns a TestScheduler, which is useful for debugging. It allows you to test schedules of events by manually advancing the clock at whatever pace you choose.
So the TestScheduler
doesn’t mean “blocking” or “on the same thread” as the rest, it just means I could manually advance the clock.
Good guy David Karnok helped me understanding the TestScheduler on twitter:
@lovisbrot Use ts.awaitTerminalEvent before asserting; store test() and call advanceTimeBy to make time move forward.
— David Karnok (@akarnokd) 8. März 2016
2
I shouldn’t forget that (in Kotlin) the subscribe
method that takes a lambda
DataSource().subscribe {
//do things
}
is only for lambdas - and not for instances of Subscriber.
This seems obvious, but it happens that while changing things from one concept to the other I might accidently keep the {}
brackets.
3
Good recources on Testing Rx: