gpt4 book ai didi

javascript - 为什么本次测试中的 Qunit 计数器始终达不到目标值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:34:35 24 4
gpt4 key购买 nike

我正在进行 Qunit 测试,但无法了解正在发生的事情。测试是否来自 jQuery Mobile 的 listview.filter 扩展。

我对变量 _refreshCornersCount 的作用一无所知曾经达到 3 的值(value).

这是有问题的部分。

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 过滤器扩展。可以找测试here

编辑::
所以这是我测试的完整代码。请注意,我正在执行与 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" );

}
]);
});
...

最佳答案

该函数在每个循环结束时调用。在调用之前

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

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

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

第三个。因此,

this._refreshCornersCount === 3

希望这对您有所帮助。

关于javascript - 为什么本次测试中的 Qunit 计数器始终达不到目标值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com