1

オブジェクトのイベント処理をテストするための最良の方法は何ですか?私はこれを理解できないようです。

いくつかのイベントリスナーを設定するオブジェクトがあり、これらのイベントが発生するのを観察すると、ページ上のdomオブジェクトが変更されます。以下の例の最後のテストが失敗する複数のテストがある場合、他のテストをコメントアウトすれば問題ありません。

私は次のようなテストスイートを持っています

var TitleTest = TestCase('TitleTest');

TitleTest.prototype.defaultTitle = 'title';
TitleTest.prototype.defaultCount = '0';

TitleTest.prototype.setUp = function() {
    var titleObj;
    this.div = new Element('div');
    $$('body').first().insert(this.div);
    this.div.insert('<div id="title"><h1><span id="titleCaption">' + this.defaultTitle + '</span><span id="titleCount">' + this.defaultCount + '</span></h1></div>');
    titleObj = new Title();
});

TitleTest.prototype.testNewItemsEvent = function() {
    var data = {count: 10};

    assertEquals('Count should be zero before events are fired', this.defaultCount, $('titleCount').innerHTML);
    document.fire('custom:NewItems', data);
    assertEquals('New count should be 10', data.count + '', $('titleCount').innerHTML);
});

// ... a few other simple tests like the one above

TitleTest.prototype.testUpdateSpecial = function() {
    var data = {caption: 'Special Title', count: 10},
        specialObj = {special: {type: 2, value: 5}};

    // Emulate a special type of category, that can only be 
    // added at page load
    document.fire('custom:UpdateTitle', data);
    assertEquals(data.caption, $('titleCaption').innerHTML);
    assertEquals(data.count, $('titleCount').innerHTML);

    //Removing the special category should revert the title to its default
    document.fire('custom:RemoveSpecial', specialObj);
    assertEquals(this.defaultTitle, $('titleCaption').innerHTML);
    assertEquals(this.defaultCount, $('titleCount').innerHTML);

    // only way to get this added back in during non load is using
    // javascript history so it needs to revert to previous state
    document.fire('custom:AddSpecial', specialObj);
    assertEquals(data.caption, $('titleCaption').innerHTML);
    assertEquals(data.count + '', $('titleCount').innerHTML);
});

前のテストが実行された場合、最後のアサートのペアは常に失敗しますが、他のテストをコメントアウトすると合格します。これを機能させるために何ができるか途方に暮れています。

編集:特別なタイトルの追加/削除を処理するコードは次のとおりです

    Event.observe(document, 'custom:UpdateTitle', function(event) {
        if (event.memo.caption) {
            this._updateCaption(event.memo.caption);
        }

        if (event.memo.count) {
            this._updateCount(event.memo.count);
        }   

    }.bind(this));


    Event.observe(document, 'custom:RemoveSpecial', function(event) {
        if (
            event.memo.special.type === 1 ||
            event.memo.special.type === 2 ||
            (   
                event.memo.special.type === 3 &&
                parseInt(event.memo.special.value, 10) === 8
            )   
        ){  
            this._previousTitle = $('titleCaption').innerHTML;
            this._resetTitle();
        }   
    }.bind(this));

    Event.observe(document, 'custom:AddSpecial', function(event) {
        if (
            event.memo.special.type === 1 ||
            event.memo.special.type === 2 ||
            (   
                event.memo.special.type === 3 &&
                parseInt(event.memo.special.value, 10) === 8
            )   
        ){  
            if (!this._previousTitle.blank()) {
                this._updateCaption(this._previousTitle);
            }   
        }   
    }.bind(this));
4

2 に答える 2

1

titleObjとそのリスナーをDOMから切り離すために、tearDownメソッドでクリーンアップコードを記述してみてください。私の知る限り、jsTestDriverは各テストの後にDOMをリセットしますが、DOMイベントをリッスンしているオブジェクトで何が起こるかはわかりません。DOMにアタッチされているため、ガベージコレクションできない古いtitleObjがまだ残っている可能性があります。

それ以外は、DOMイベントをテストする方法は問題ないように見えます。DOMイベントを発生させ、コードが想定どおりに動作したかどうかをアサートします...

于 2011-07-06T09:33:03.757 に答える
1

@meyerteeは私の問題の解決をほのめかしました。セットアップとティアダウンはPHPUnitのように機能し、すべてのテストの実行前と実行後にそれぞれ起動されると想定しましたが、jsTestDriverは各テストの前後に起動します。セットアップでDOMとTitleイベントをセットアップしていたため、各テストの前に新しいタイトルオブジェクトが作成され、より多くのリスナーが競合状態を引き起こしていました。Titleオブジェクトのインスタンス化をセットアップから移動しましたが、これで問題が修正されました。

于 2011-07-11T13:53:48.713 に答える