为了测试Jquery Ajax响应,我正在使用模拟ajax和Qunit。我必须断言Mock Ajax响应,但在我的测试用例中,Assert语句首先运行,然后我得到Mock ajax的响应。
如何在Qunit中调用assert语句之前确保mock ajax响应可用
答案 0 :(得分: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
});
...
});