4

私は Qunit テストを行っていますが、何が起こっているのかを理解するのに苦労しています。jQuery Mobile の listview.filter 拡張からの if のテスト。

_refreshCornersCount変数が の値にどのように到達するかについては、私にはわかりません3

ここが問題の部分です。

module( "Custom search filter", {
    setup: function() {
        var self = this;
          // initially set to 0
          this._refreshCornersCount = 0;
          this._refreshCornersFn = 
            $.mobile.filterbar.prototype._addFirstLastClasses;

        this.startTest = function() {
          return this._refreshCornersCount === 1;
        };

        // _refreshCorners is the last method called in the filter loop
        // so we count the number of times _refreshCorners gets invoked 
        // to stop the test
        $.mobile.filterbar.prototype._addFirstLastClasses = function() {
            // increase by 1
            self._refreshCornersCount += 1;
            self._refreshCornersFn.apply( this, arguments );
            if ( self.startTest() ) {
                start();
            }
        }
    },
    teardown: function() {
        $.mobile.filterbar.prototype._refreshCorners = this._refreshCornersFn;
    }
});

asyncTest( "Custom filterCallback iterates on all list elements",
    function(){
        var listPage = $( "#search-customfilter-test" ),
          filterCallbackCount = 0,
          expectedCount = 2 * listPage.find("li").length;
        expect( 1 );

        this.startTest = function() {
            // trigger once _refreshCornersCount reaches 3
            if ( this._refreshCornersCount === 3 ) {
            equal(
                filterCallbackCount,
                expectedCount,
                "filterCallback called "+ expectedCount +" times"
            );
        }
        // return true/false
        return this._refreshCornersCount === 3;
    }

    $.testHelper.pageSequence( [

        ...
        // triggers a change,
        // which will triggers a filter loop
        listPage.find( "input" ).val( "at" ).trigger( "change" );
        listPage.find( "input" ).val( "atw" ).trigger( "change" );
        }
    ]);
});

必要な部分はすべてそこにあるので、テストから一部のコードを省略しました。

私のマシンでは、テストはトリガーされません。これthis._refreshCornersCountは、 が に設定され0、2 つの入力変更で 2 倍に増加し、それぞれがフィルター ループをトリガーするためです。したがって、すべてのパラメーター (私がテストしているものを含む) は正しく、テストのみが起動することはありません。this._refreshCornersCount = 2

質問:
私はしばらくこれに取り組んでいます...おそらく明らかな何かが欠けているので、上記のコード スニペットのみから のthis._refreshCornersCount値に到達するための技術的な方法はありますか?3

EDIT : テストは jQuery Mobile のテストであり、具体的には listview フィルター拡張機能です。ここでテストを見つけることができます

編集::ここ
に私のテストの完全なコードがあります。JQMと同じセットアップを行っていることに注意してください。

module( "Custom search filter", {
    setup: function() {
        var self = this;
            this._refreshCornersCount = 0;
            this._refreshCornersFn = $.mobile.filterbar.prototype._addFirstLastClasses;
            this.startTest = function() {
                return this._refreshCornersCount === 1;
            };

        // _refreshCorners is the last method called in the filter loop
        // so we count the number of times _refreshCorners gets invoked to stop the test
        $.mobile.filterbar.prototype._addFirstLastClasses = function() {
            self._refreshCornersCount += 1;
            self._refreshCornersFn.apply( this, arguments );
            if ( self.startTest() ) {
                start();
            }
        }
    },
    teardown: function() {
        $.mobile.filterbar.prototype._refreshCorners = this._refreshCornersFn;
    }
});

asyncTest( "Custom filterCallback should cause iteration on all list elements", function(){
    var listPage = $( "#search-customfilter-test" ),
        filterCallbackCount = 0,
        expectedCount = 2 * listPage.find("li").length;
    expect( 1 );

    this.startTest = function() {
    // XXX NOTE: changed this to 2x, because two changes, trigger 2x _onKeyUp
    // and 2x addFirstLastClasses. I never reach 3 with the code as-is
    if ( this._refreshCornersCount === 2 ) {
        equal( filterCallbackCount, expectedCount, "filterCallback should be called exactly "+ expectedCount +" times" );
    }
    return this._refreshCornersCount === 2;
}

$.testHelper.pageSequence( [
    function(){
        //reset for relative url refs
        $.mobile.changePage( home );
    },

    function() {
        $.mobile.changePage( "#search-customfilter-test" );
    },

    function() {
        // set the listview instance callback
        listPage.find( "ul" ).filterbar( "option", "filterCallback", function( text, searchValue, item ) {
            filterCallbackCount += 1;

            return text.toString().toLowerCase().indexOf( searchValue ) === -1;
        });

        // trigger a change in the search filter
        listPage.find( "input" ).val( "at" ).trigger( "change" );
        listPage.find( "input" ).val( "atw" ).trigger( "change" );

        }
    ]);
});
...
4

1 に答える 1

1

この関数は、各サイクルの終わりに呼び出されます。の呼び出しの前に

listPage.find( "input" ).val( "at" ).trigger( "change" );

はすでに一度呼び出されています。前の行で 2 回目、最後の行で

listPage.find( "input" ).val( "atw" ).trigger( "change" );

第3。したがって、

this._refreshCornersCount === 3

お役に立てれば。

于 2013-06-26T17:07:53.370 に答える