Qun ajax上的Qunit单元测试错误 [英] Qunit unit test error , on jQuery ajax

查看:54
本文介绍了Qun ajax上的Qunit单元测试错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为ajax suing Qunit编写了单元测试,但收到错误如

I have written unit test for ajax suing Qunit, but getting error like

Error: assertion outside test context, was .success@http://test.loc/assets/test/widget-add-or-edit-test.js:227 
b.Callbacks/c@http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3 
b.Callbacks/p.fireWith@http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3
k@http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:5 .send/r@http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:5
Source:     

http://code.jquery.com/qunit/qunit-1.11.0.js:899

我的测试代码是

test( "Widget.updateAxisTypeAjax x", function() {

        stop();

        Widget.updateAxisTypeAjax( {   
                axis   : 'x' ,

                x_id   : 179,
                y_id   : 175
            }  ,{
            success : function( response ){

                ok( true, "updateAxisTypeAjax Success PASS!");
                equal( typeof response , 
                       "object" , 
                       "updateAxisTypeAjax response is json valid object !"
                );

                equal( typeof response == "object" && 
                       ( "average" in response  ) && 
                       ("is_datetime" in response) , 
                        true , 
                       "updateAxisTypeAjax average check PASS !"
                );

            } ,
            complete : function(){
                ok(true, "updateAxisTypeAjax completed PASS!");
                start();
            }
        });

});

Widget.updateAxisTypeAjax

Widget.updateAxisTypeAjax = function( data_obj, callbacks ){

        data_obj = jQuery.extend( data_obj ,{
            action : 'update_axis'
        });

        Widget.ajax( 5 )
              .data( data_obj  )
              .callbacks( callbacks )
              .fire();

}

Widget.ajax 是:

var Widget = Widget || {} ;


function _WidgetAjax( type  ){

        var loader      = $('#series_loader');

        this.ajaxUrl = '/dashboard/charts/ajax/' + ( type || 1 ) ;

        this.ajaxSettings = {

            url: this.ajaxUrl ,
            type:"GET",
            data :{} ,
            complete : function(){ // always
                  loader.hide();
            }
        };

        // show ajax loading
        loader.show();

        this.data = function( data ){

                jQuery.extend( this.ajaxSettings, { data: data }  );
                return this;
        }

        this.callbacks = function( callbacks ){

                jQuery.extend( this.ajaxSettings, callbacks || {}  );

                return this;
        }

        this.success = function( func ){

                if ( jQuery.isFunction( func ) ){
                     jQuery.extend( this.ajaxSettings, { success: func }  );
                }

                return this;
        };

        this.fire = function(){

                return $.ajax( this.ajaxSettings );
        };

};

Widget.ajax = function( type ){ 

        return new _WidgetAjax( type );        
};

请帮我修复此单元测试错误!

Please help me fix this unit test error !

推荐答案

您正在测试异步函数,因此需要使用QUnit中的异步功能。

You're testing an asynchronous function, so you need to use the async features in QUnit.

而不是 test 你应该使用 asyncTest

这是< a href =http://qunitjs.com/cookbook/#asynchronous-callbacks =nofollow>文档。

请注意,你有告诉QUnit在进行异步测试时会有多少断言。

Note that you have to tell QUnit how many assertions to expect when doing async tests.

asyncTest( "Widget.updateAxisTypeAjax x", 4, function() {

        stop();

        Widget.updateAxisTypeAjax( {   
                axis   : 'x' ,

                x_id   : 179,
                y_id   : 175
            }  ,{
            success : function( response ){

                ok( true, "updateAxisTypeAjax Success PASS!");
                equal( typeof response , 
                       "object" , 
                       "updateAxisTypeAjax response is json valid object !"
                );

                equal( typeof response == "object" && 
                       ( "average" in response  ) && 
                       ("is_datetime" in response) , 
                        true , 
                       "updateAxisTypeAjax average check PASS !"
                );

            } ,
            complete : function(){
                ok(true, "updateAxisTypeAjax completed PASS!");
                start();
            }
        });

});

应该可行。这不应该太难修复!

Should probably work. This shouldn't be too hard to fix!

这篇关于Qun ajax上的Qunit单元测试错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆