2016-05-02 4 views
7

私はfluxを実装するためにrxjs5を使用するwebappプロジェクトを持っていますが、現在はユニットテストを書くためのソリューションを探しています。大理石テストrxjs5メソッドを別のプロジェクトの中で使用する

実際に、私は例えば、内部のカスタムの観測を実施しています:

function getActivityObservable(events, timeout) { 
    return Observable.create((observer) => { 
    const deb = debounce(() => observer.next(false), timeout || DEFAULT_TIMEOUT); 
    const sub = events.subscribe((e) => { 
     if (!e) { 
     deb.cancel(); 
     observer.next(false); 
     } else { 
     observer.next(true); 
     deb(e); 
     } 
    }); 

    return() => { 
     if (sub) sub.unsubscribe(); 
     if (deb) deb.cancel(); 
    }; 
    }).distinctUntilChanged(); 
} 

を私はmarble testing wayを使用して、それをテストし、のようなものを書きたい(私はrxjsリポジトリからサンプル例を取った)

describe("getActivityObservable",() => { 
    it("should debounce by selector observable",() => { 
     const e1 = hot("--a--bc--d----|"); 
     const e1subs = "^    !"; 
     const expected = "----a---c--d--|"; 

     expectObservable(e1.debounce(getTimerSelector(20))).toBe(expected); 
     expectSubscriptions(e1.subscriptions).toBe(e1subs); 
    }); 
    }); 

私の質問は次のとおりです。

は、hotのようなオペレーターと(を大理石の試験方法を使用することが可能ですなど)をrxjs5プロジェクトの外側に配置します。私は私のプロジェクトでこの素敵なツールを使う方法を理解していません。

ありがとうございました。

+1

あなたはすることができますが、それは非常に現時点では人間工学ではありません。基本的には、 'TestScheduler'のインスタンスが必要で、' createHotObservable'メソッドと 'expectObservable'メソッドとそのすべてがあります。次に、スケジューラー上で 'flush()'を呼び出してアサートさせます。 –

+0

ありがとう、ありがとう。このメソッドを使用して現在の観測値をテストすることをお勧めしますか?あるいは、大理石のテストを将来公開する予定ですか? –

+0

あなたは確かにこのメソッドを使って観測値をテストすることができます。まあまあ、人間工学にはあまり似ていません。 –

答えて

3

あなたはBenとしてコメントすることができます。「それは人間工学的ではありません。

私はitにパッチを適用モカやサルを使用しています:

const isEqual = require('lodash.isequal'); 
const TestScheduler = require('rxjs/testing/TestScheduler').TestScheduler; 

const assertDeepEqualFrame = (actual, expected) => { 
    if (!isEqual(actual, expected)) { 
    throw new Error('Frames not equal!'); 
    } 
} 

const oit = global.it; 
global.it = function(description, cb, timeout) { 
    if (cb.length === 0) { 
    oit(description, function() { 
     global.rxTestScheduler = new TestScheduler(assertDeepEqualFrame); 
     cb(); 
     global.rxTestScheduler.flush(); 
    }); 
    } else { // async test 
    oit.apply(this, arguments); 
    } 
}; 

私はngrx/storeからインスピレーションをたくさん取って、特に、このファイルていますhttps://github.com/ngrx/store/blob/master/spec/helpers/test-helper.ts

し、私は次のように私のテストを書くことができます。

it('should filter with an always-true predicate',() => { 
    const source = hot('-1--2--^-3-4-5-6--7-8--9--|'); 
    const expected =   '--3-4-5-6--7-8--9--|'; 
    const predicate =() => { return true; }; 

    expectObservable(source.filter(predicate)).toBe(expected); 
}); 

編集 ますCどのように私はサルのパッチitを参照してください:https://github.com/tjoskar/ng2-lazyload-image/blob/5e1c64a3611530ce26857a566b2d76dff890a3c5/test/helpers/test-helper.ts

+0

間違いなく私は何が必要です。どうも。 –

関連する問題