2013-04-10 113 views
3

我已经写单元测试AJAX起诉Qunit,但得到错误,如Qunit单元测试错误,jQuery的阿贾克斯

Error: assertion outside test context, was [email protected]://test.loc/assets/test/widget-add-or-edit-test.js:227 
b.Callbacks/[email protected]://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3 
b.Callbacks/[email protected]://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:3 
[email protected]://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js:5 .send/[email protected]://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);   
}; 

请帮我解决这个单元测试错误!

回答

2

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

而不是test你应该使用asyncTest

这里是documentation

请注意,您必须告知QUnit执行异步测试时需要多少断言。

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

}); 

应该可能工作。这不应该太难解决!

+1

它没有修复问题! :( – rab 2013-04-10 09:34:02