Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's write test code of RxJava!!

sasami
September 04, 2018

Let's write test code of RxJava!!

a road to write test using RxJava successfully🐱
at Android Lunch&Learn, 2018/08/23

sasami

September 04, 2018
Tweet

More Decks by sasami

Other Decks in Programming

Transcript

  1. Let's write test code of RxJava!! 1 ~a road to

    write test using RxJava successfully @sasami! 1
  2. Steps of writing test code 1. write test function 2.

    run test 3. test passed!!!!! ...finish? →There are some cases tests are passed without actually testing...!(especially for RxJava") 4
  3. @Test fun checkoutValidations_should_be_VALID_if_hugaProcessor_is_0() { viewModel.checkoutValidations.subscribe { status -> status ==

    CheckoutViewModel.ValidationStatus.VALID } viewModel.hugaProcessor.onNext(0) whenever(service.observeHoge()).thenReturn(Flowable.just(mock())) } 6
  4. ! 8

  5. What are problems? 4 using subscribe() method 4 subscribe() runs

    asynchronously even in the test code 4 an assertion inside subscribe() is occurred after evaluating test function 9
  6. Case2 @Mock private lateinit var hugaProcessor: BehaviorProcessor<Int> @Test fun `checkoutValidations

    should have VALID status if hugaProcessor is 0`() { val testSubscriber = TestSubscriber<CheckoutViewModel.ValidationStatus>() viewModel.checkoutValidations.subscribe(testSubscriber) viewModel.hugaProcessor.onNext(0) testSubscriber .assertNoErrors() .assertNotComplete() .assertValue(CheckoutViewModel.ValidationStatus.VALID) } 13
  7. Case2 @Mock private lateinit var youPay: BehaviorProcessor<Int> @Test fun `checkoutValidations

    should have VALID status if hugaProcessor is 0`() { val testSubscriber = TestSubscriber<CheckoutViewModel.ValidationStatus>() viewModel.checkoutValidations.subscribe(testSubscriber) viewModel.hugaProcessor.onNext(0) testSubscriber .assertNoErrors() .assertNotComplete() .assertValue(CheckoutViewModel.ValidationStatus.VALID) } This test is still failed...! 14
  8. what are problems? 4 check instance of BehaviorProcessor exists 4

    If the instance is mocked, onNext() does not emit any value 16
  9. Case3 @Test fun callSomeObsevable_should_be_true_if_postTransaction_is_executed() { .... viewModel.callSomeObsevable.subscribe { result ->

    Assert.assertTrue(result) } viewModel.postTransaction(itemId).test() .assertSubscribed() .assertNoErrors() .assertComplete() } →we want to make sure callSomeObsevable emits true if postTransaction() is called successfully. 18
  10. What are problems? 4 test() method also runs synchronously. So

    we have to care about the order of the code. 4 So postTransaction() method does not call when assertValue of the first stream is called. 21
  11. How to fix this? @Test fun callSomeObsevable_should_be_true_if_postTransaction_is_executed() { .... val

    testSubscriber = viewModel.callSomeObsevable.test() viewModel.postTransaction(itemId).test() .assertSubscribed() .assertNoErrors() .assertComplete() testSubscriber .assertNoErrors() .assertComplete() .assertValue(true) } →before calling postTransaction, we need to subscribe observable function first. 22
  12. Overview! 4 Use TestSubscriber() 4 don't mock BehaviorProcessor if you

    want to get from value from it. 4 care about order of the code where subscriber is subscribed 23