Qunit断言mockajax的回应

时间:2013-04-18 09:15:11

标签: jquery mocking qunit

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

如何在Qunit中调用assert语句之前确保mock ajax响应可用

1 个答案:

答案 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
  });
  ...
});

还有一些documentation of Async control in QUnit