4

我正在参加 Qunit 测试,无法了解正在发生的事情。测试 if 来自 jQuery Mobile 的 listview.filter 扩展。

我对变量如何_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 倍,每个都触发一个过滤器循环。所以所有参数(包括我正在测试的参数)都是正确的,只有测试永远不会触发,因为this._refreshCornersCount = 2

问题
我现在坐了一会儿......也许我错过了一些明显的东西,所以:有没有一种技术方法可以仅从上面的代码片段this._refreshCornersCount中达到价值?3

编辑:该测试是来自 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" );

已经被调用过一次。上一行是第二次,最后一行

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

第三。因此,

this._refreshCornersCount === 3

希望这可以帮助。

于 2013-06-26T17:07:53.370 回答