$30 off During Our Annual Pro Sale. View Details »

RxSwiftのテスト入門

100059
April 16, 2019

 RxSwiftのテスト入門

RxSwiftのコードへのテストの書き方の流れ

100059

April 16, 2019
Tweet

More Decks by 100059

Other Decks in Technology

Transcript

  1. RxSwiftͷςετೖ໳ iOS Test Night #10 2019/04/16

  2. Akihisa Sengoku גࣜձࣾαΠόʔΤʔδΣϯτ AmebaʢΞϝʔόʣ iOSΤϯδχΞ

  3. Amebaͷ࠷ۙͷ։ൃ ʢiOS Teamʣ

  4. 2018 + MVVM 2019 RxͷςετΛ ॻ͖࢝ΊΔ MVC MVVM etc…

  5. RxSwiftͰͷςετ͸ͳ͔ͳ͔ ೉͍͠ɻɻɻ

  6. ຊ೔ͷ಺༰ 3Y4XJGUͷίʔυ΁ͷ ςετͷॻ͖ํͷྲྀΕ ʢͬ͘͟Γͱʣ 3Y4XJGUͰͷςετͷཧղ͕͕͋Ε͹ʜ

  7. ςετΛॻ͘ର৅ จࣈҎ্Ͱొ࿥͢Δ͜ͱ͕Մೳͳ ؆୯ͳϑΥʔϜ

  8. 7JFX ඳը 7JFX.PEFM ϩδοΫ ςΩετೖྗ Ϙλϯͷ &OBCMF ߦະຬ ˠ'BMTF ̏ߦҎ্

    ˠ5SVF RxSwiftΛ࢖ͬͨMVVMͰͷVM૚ʹςετΛॻ͘͜ͱΛ૝ఆ
  9. 7JFX ඳը 7JFX.PEFM ϩδοΫ ςΩετೖྗ Ϙλϯͷ &OBCMF ߦະຬ ˠ'BMTF ̏ߦҎ্

    ˠ5SVF ୯ମςετͰ͖ͦ͏ʁʁ RxSwiftΛ࢖ͬͨMVVMͰͷVM૚ʹςετΛॻ͘͜ͱΛ૝ఆ
  10. 7JFX ඳը 7JFX.PEFM ϩδοΫ ςΩετೖྗ Ϙλϯͷ &OBCMF ߦະຬ ˠ'BMTF ̏ߦҎ্

    ˠ5SVF ୯ମςετͰ͖ͦ͏ʁʁ RxSwiftΛ࢖ͬͨMVVMͰͷVM૚ʹςετΛॻ͘͜ͱΛ૝ఆ
  11. 7JFX ඳը 7JFX.PEFM ϩδοΫ 0CTFSWBCMF4USJOH 0CTFSWBCMF#PPM ςΩετೖྗ Ϙλϯͷ &OBCMF 

       ' ' 5 5
  12. 7JFX ඳը 7JFX.PEFM ϩδοΫ 0CTFSWBCMF4USJOH 0CTFSWBCMF#PPM ςΩετೖྗ Ϙλϯͷ &OBCMF 

       ' ' 5 5 0CTFSWBCMF͸Ұ࿈ͷ஋ͷྲྀΕ ετϦʔϜ Ͱ͋Γɺ ୯Ұͷ஋ͱͯ͠ςετ͢Δ͜ͱ͕ࠔ೉
  13. 7JFX ඳը 7JFX.PEFM ϩδοΫ 0CTFSWBCMF4USJOH 0CTFSWBCMF#PPM ςΩετೖྗ Ϙλϯͷ &OBCMF 

       ' ' 5 5 ʲ*OQVUʳΠϕϯτͷ࣌ؒͱ஋Λࢦఆͯ͠ɺ 0CTFSWBCMFʹ஋Λྲྀ͢͜ͱ͕ࠔ೉
  14. 7JFX ඳը 7JFX.PEFM ϩδοΫ 0CTFSWBCMF4USJOH 0CTFSWBCMF#PPM ςΩετೖྗ Ϙλϯͷ &OBCMF 

       ' ' 5 5 ʲ0VUQVUʳྲྀΕͨΠϕϯτͷ࣌ؒͱ஋Λ ·ͱΊͯऔಘ͢Δ͜ͱ͕ࠔ೉
  15. 7JFX ඳը 7JFX.PEFM ϩδοΫ 0CTFSWBCMF4USJOH 0CTFSWBCMF#PPM ςΩετೖྗ Ϙλϯͷ &OBCMF 

       ' ' 5 5 ࣮࣌ؒͰಈ͍͍ͯΔͨΊɺ ςετʹ͕͔͔࣌ؒͬͯ͠·͏ɻ
  16. RxSwiftͷObservableΛ ͲͷΑ͏ʹ ςετ͢Δͷ͔ʁʁ

  17. RxTest RxSwiftͷϨϙδτϦʔ಺ʹ͋ΔRxSwiftͷͨ ΊͷςετϑϨʔϜϫʔΫ ઌ΄Ͳͷ໰୊Λղܾ͢ΔͨΊʹ TestSchedulerͱ͍͏ಛผͳScheduler͕͋Δ

  18. TestScheduler • Ծ૝࣌ؒΛج४ͱͨ͠ObservableͱObserverͷϞοΫͰ͋Δ TestableObservableͱTestableObserverΛੜ੒͢Δɻ ← ࣮࣌ؒͰͷςετ໰୊Λղܾ • TestableObservable͸ɺ࡞੒࣌ʹྲྀ͢Πϕϯτͷ஋ͱ࣌ؒΛࢦ ఆͯ͠ɺྲྀ͢͜ͱ͕Ͱ͖Δɻ ←

    InputΛղܾ • TestableObserver͸ɺΠϕϯτͷ࣌ؒͱ஋Λه࿥͢Δ͜ͱ͕Ͱ ͖Δ ← OutputΛղܾ
  19. 5FTU4DIFEVMFS 5FTUBCMF0CTFSWBCMF 5FTUBCMF0CTFSWFS ࡞੒

  20. 5FTU4DIFEVMFS 5FTUBCMF0CTFSWBCMF 5FTUBCMF0CTFSWFS ࡞੒ ࡞੒࣌ʹΠϕϯτΛ ొ࿥Մೳ Πϕϯτͷऔಘ   

     ' ' 5 5
  21. 5FTU4DIFEVMFS 5FTUBCMF0CTFSWBCMF 5FTUBCMF0CTFSWFS ࡞੒ ࡞੒࣌ʹΠϕϯτΛ ొ࿥Մೳ Πϕϯτͷऔಘ   

     ' ' 5 5 ࣌ؒͱ*OQVUͱ0VUQVUͷ໰୊Λղܾʂ
  22. 5FTU4DIFEVMFS 5FTUBCMF0CTFSWBCMF 5FTUBCMF0CTFSWFS ࡞੒ ࡞੒࣌ʹΠϕϯτΛ ొ࿥Մೳ Πϕϯτͷऔಘ   

     ' ' 5 5 ΠϕϯτΛͲͷΑ͏ʹѻ͏ͷ͔ʁʁ
  23. Recorded /// Record of a value including the virtual time

    it was produced on. public struct Recorded<Value>: CustomDebugStringConvertible { /// Gets the virtual time the value was produced on. public let time: TestTime /// Gets the recorded value. public let value: Value public init(time: TestTime, value: Value) { self.time = time self.value = value } } ΠϕϯτͰੜ੒͞Εͨ஋ͱ ஋͕ੜ੒͞ΕͨԾ૝࣌ؒΛอ࣋ɻ
  24. Recorded ΠϕϯτͰੜ੒͞Εͨ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 <3FDPSEFE>
  31. RxTestͷTestSchedulerʹΑͬͯ RxSwiftͷObservableͷςετ͕ Մೳʹʂʂ

  32. ·ͱΊ • RxSwiftͷObservableΛςετ͢Δͷ͸ࠔ೉ • ObservableΛςετ͢ΔͨΊʹRxSwiftͷ TestSchedulerΛ࢖༻ • TestSchedulerʹΑͬͯTestableObservableͱ TestableObserverΛ࡞੒ͯ͠ςετ͢Δ