1

我正在尝试测试 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。请帮忙 !

4

1 回答 1

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();
        }
    });

});

有效 !

于 2013-03-12T15:50:29.357 回答