2013-04-18 44 views
0

为了测试Jquery Ajax响应,我使用了Qunit模拟ajax。我必须断言模拟Ajax响应,但在我的测试案例中,Assert语句首先运行,然后我从Mock ajax获得响应。Qunit Assert mockajax响应

如何确保模拟Ajax响应是调用断言语句Qunit

回答

1

你需要做一个asyncTest相对于简单的test调用之前可用。以下是我使用的一个示例:

... 
asyncTest("test title", function() { 
    $.mockjaxClear(); // clear any existing mock jax entries (could be in a setup method) 
    $.mockjax({ // pass in your request matcher/response object 
    url: '/some/file.php', 
    type: 'post', 
    status: 200, 
    dataType: 'json', 
    response: function(req) { 
     this.responseText = JSON.stringify({some: "data"}); 
    } 
    }); 

    $.ajax({ 
    url: '/some/file.php', 
    type: 'post', 
    dataType: 'json', 
    success: function(d) { 
     a.deepEqual(d, {some: "data"}, "Object data is correct in callback"); 

     // other tests 

     start(); // this tells QUnit to start back up, async is done 
    }); 
    ... 
}); 

还有一些documentation of Async control in QUnit