RxCookbook
RxCookbook copied to clipboard
WIP: Unit testing
I'm assembling info about what to test and how to test it before a write-up. Input, links, strategies, and techniques very welcome.
Approaching tests
Rx-specific things to test that make sense to me so far:
- complex transformations from
Observable<A>toObservable<B>- in general use a subscription to test the transformation
- use
RxTest.TestScheduler.createObserverto record the output for a controlled set of inputs - use
RxTest.TestScheduler.startwith a closure to do something when signals come in, like transforming on the fly (to test your own operators) - consider encapsulating this in a view model if you do this for UI code
- subscriptions with and without intermediate transformations
- mock whatever
subscribe(onNext: { ... })delegates to and write assertions about what arrives there - consequently, don't worry about simple transformations from input
Observableup until the subscription; you don't have to extract these into testable transformations from A to B. Instead, you can write your delegation assertions with the expected output value of type B, considering intermediate steps as implementation detail that mustn't be tested. - for complex transformations, consider extracting everything you have to do right up until
subscribe()(see (1))
- mock whatever
Resources
- https://github.com/ReactiveX/RxSwift/blob/master/Documentation/UnitTests.md -- how to test
Observables and useTestSchedulerorRxBlocking - http://rx-marin.com/post/rxswift-rxtests-unit-tests/
- http://rx-marin.com/post/rxswift-rxtests-unit-tests-part-2/
Other Rx's:
- https://xgrommx.github.io/rx-book/content/getting_started_with_rxjs/testing_and_debugging.html
- https://medium.com/mobile-engineering/rxjava-unit-testing-examples-a26af80dfce4#.y2m4fs218 -- side effects of starting/finishing a process during subscriptions
- https://labs.ribot.co.uk/unit-testing-rxjava-6e9540d4a329#.dkr8b8gkr -- comparing ways to test
Observables - https://medium.com/azimolabs/testing-rx-code-7918d7ee1680#.8ngy2u4lt -- Java sample with a test double
Observer