Javascript QUnit模拟自动完成
在我的QUnit测试中,我想模拟自动完成方法(jQuery UI),但每次运行测试时,我都会这样做:Javascript QUnit模拟自动完成,javascript,jquery-ui,unit-testing,qunit,Javascript,Jquery Ui,Unit Testing,Qunit,在我的QUnit测试中,我想模拟自动完成方法(jQuery UI),但每次运行测试时,我都会这样做: test("Create_PassedContainer_RunsAutocompleteOnMatchingElement",function(){ var $matchingInput = $('<input data-autocomplete-url="some"/>'); var $dom = $('<div><input/></
test("Create_PassedContainer_RunsAutocompleteOnMatchingElement",function(){
var $matchingInput = $('<input data-autocomplete-url="some"/>');
var $dom = $('<div><input/></div>');
$dom.append($matchingInput);
var autocompleteWasCalled = false;
$matchingInput.autocomplete = function(){ autocompleteWasCalled = true; };
new Autocomplete($dom);
ok(autocompleteWasCalled,"Should call autocomplete.");
});
测试中的代码:
function Autocomplete($container) {
var $self = this;
this.Initialize = function($container) {
$self.$container = $container;
$self.$text = $('*[data-autocomplete-url]', $container);
$self.$value = $('input[type="hidden"]', $container);
$self.$text.autocomplete();
};
$self.Initialize($container);
};
一切都会有帮助的 自动完成是一个插件,请尝试以下操作:
var oldAutocomplete = $.fn.extend(true, {}, $.autocomplete);
$.autocomplete = function(){
autocompleteWasCalled = true;
}
/// Other code...
$.fn.autocomplete = oldAutocomplete;
就是这样。非常感谢你!
var oldAutocomplete = $.fn.extend(true, {}, $.autocomplete);
$.autocomplete = function(){
autocompleteWasCalled = true;
}
/// Other code...
$.fn.autocomplete = oldAutocomplete;