我正在尝试测试jquery ajax请求
Widget.seriesEditLoadSeriesAjax = function( id , jqXHRCallbacks ) {
var ajax_url = '/dashboard/charts/ajax/2';
var data_obj = {
SID : id || 0 ,
getSeries : true
};
var jqxhr = $.ajax({
url : ajax_url,
type : "GET",
data : data_obj
});
jqXHRCallbacks = jqXHRCallbacks || {
done : Widget.seriesEditLoadSeriesCallback ,
always : function(){
$('#series_loader').hide();
}
};
jQuery.extend( jqxhr, jqXHRCallbacks );
console.log( 'jqxhr.always', jqxhr.always );
console.log( 'jqxhr.done', jqxhr.done );
}
使用qUnit库
为它编写测试test( "Widget.seriesEditLoadSeriesAjax", function() {
stop();
Widget.seriesEditLoadSeriesAjax( 67,{
done : function(){
ok( true, "asynchronous Success !");
start();
} ,
always : function(){
ok(true, "asynchronous completed PASS!");
start();
}
});
});
但它总是返回结果
asynchronous completed PASS!
为什么省略done
的测试。请帮忙!
答案 0 :(得分:1)
修改功能
Widget.seriesEditLoadSeriesAjax = function( id , callbacks ) {
var ajax_url = '/dashboard/charts/ajax/2';
var data_obj = {
SID : id || 0 ,
getSeries : true
};
var ajaxSettings = {
url : ajax_url,
type : "GET",
data : data_obj ,
success : Widget.seriesEditLoadSeriesCallback ,
complete : function(){
$('#series_loader').hide();
},
error : function(){
}
};
jQuery.extend( ajaxSettings, callbacks );
var jqxhr = $.ajax( ajaxSettings );
}
并将测试代码更改为
test( "Widget.seriesEditLoadSeriesAjax", function() {
stop();
Widget.seriesEditLoadSeriesAjax( 67,{
success : function( response ){
ok( true, "asynchronous Success !");
} ,
complete : function(){
ok(true, "asynchronous completed PASS!");
start();
}
});
});
有效!