RxCookbook icon indicating copy to clipboard operation
RxCookbook copied to clipboard

WIP: Unit testing

Open DivineDominion opened this issue 9 years ago • 0 comments

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:

  1. complex transformations from Observable<A> to Observable<B>
    • in general use a subscription to test the transformation
    • use RxTest.TestScheduler.createObserver to record the output for a controlled set of inputs
    • use RxTest.TestScheduler.start with 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
  2. 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 Observable up 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))

Resources

  • https://github.com/ReactiveX/RxSwift/blob/master/Documentation/UnitTests.md -- how to test Observables and use TestScheduler or RxBlocking
  • 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

DivineDominion avatar Dec 01 '16 15:12 DivineDominion