Your SlideShare is downloading. ×
Testing RxJS
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Testing RxJS

101
views

Published on

RxJSでユニットテストを書く

RxJSでユニットテストを書く

Published in: Engineering

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
101
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. testing RxJS えるきち
  • 2. 誰だおまえ • Dwangoの片隅で色々な言語さわってる • JSは今年になってさわり始めた • ID: erukiti だったり erukiti_ だったり
  • 3. Caution! • 注意: CoffeeScript • 注意: 試行錯誤中
  • 4. ユニットテストに必要なモノ • テスト内容に即した初期stream • In/Outがはっきりしてて副作用の無い関数 • 具体的にはstreamを受けてオペレータで処理 後のstreamを返すようなもの • 関数実行後のstreamの中身判定
  • 5. 簡単な計算をしてみる compute = (stream) -> stream .map (x) -> x * 2 ! # 流れてくるデータを2倍するだけのもの
  • 6. 初期ストリームは簡単 Rx.observable.of(1, 2, 3, 4) ! # 単に適当な数値を並べるだけ
  • 7. どうやって判定するか res = compute(Rx.observable.of(1, 2, 3, 4)) ! res.toArray().subscribe (x) -> assert(x == [2, 4, 6, 8]) ! # 配列の比較できないのね…
  • 8. Rx.internals.isEqual collectionAssert = (expected, actual) -> if expected.length != actual.length assert(false, 'Not equal length.') ! isOk = true for i in [0 .. expected.length] isOk = Rx.internals.isEqual(expected[i], actual[i]) break unless isOk ! assert(isOk)
  • 9. うまくいった res = nCompute(Rx.Observable.of(1, 2, 3, 4)) ! res.toArray().subscribe (x) -> collectionAssert(x, [2, 4, 6, 8]) ! # 時間軸が絡まないならこれでOKかな
  • 10. よくあるダブルクリック判定 nClick = (stream) -> stream .buffer stream.throttle(250) .map (x) -> x.length .filter (n) -> n >= 2
  • 11. 初期stream (ちょっと長い) onNext = Rx.ReactiveTest.onNext onCompleted = Rx.ReactiveTest.onCompleted ! scheduler = new Rx.TestScheduler() xs = scheduler.createHotObservable( onNext(250, true) onNext(300, true) onNext(1000, true) ) ! res = scheduler.startWithTiming(() -> nClick(xs) , 50, 150, 1600)
  • 12. 50ms 生成 150ms subscribed 250ms クリック 300ms クリック 550ms throttleが切れてダブルクリック判定が生じる 1000ms クリック 1250ms throttleが切れるがダブルクリックにならず 1600ms disposed
  • 13. そのままではうまくいかない • なぜかonCompleteを発行しないと何も起きない • onCompleteを発行してもなぜかトリプルクリッ クになる • throttleオペレータにschedulerを渡す必要あり
  • 14. 完成形 #! /usr/bin/env coffee assert = require('assert') Rx = require('rx') collectionAssert = (expected, actual) -> if expected.length != actual.length assert(false, 'Not eual length.') isOk = true for i in [0 .. expected.length] isOk = Rx.internals.isEqual(expected[i], actual[i]) break unless isOk assert(isOk) nClick = (stream, scheduler) -> stream .buffer stream.throttle(250, scheduler) .map (x) -> x.length .filter (n) -> n >= 2 onNext = Rx.ReactiveTest.onNext onCompleted = Rx.ReactiveTest.onCompleted scheduler = new Rx.TestScheduler() xs = scheduler.createHotObservable( onNext(250, true) onNext(300, true) onNext(1000, true) ) res = scheduler.startWithTiming(() -> nClick(xs, scheduler) , 50, 150, 1600) collectionAssert(res.messages, [ onNext(550, 2) ])
  • 15. 完成形 • 長いので https://gist.github.com/erukiti/ 82c43c722d65df273783 に貼っておいた
  • 16. 参考リンク • https://github.com/Reactive-Extensions/ RxJS/blob/master/doc/gettingstarted/ testing.md • https://github.com/Reactive-Extensions/ RxJS/tree/master/doc/api/testing