0

To test the Jquery Ajax reponse, I am using mock ajax with Qunit. I have to assert the Mock Ajax reponse, but in my test case,Assert statements are running first and then i am getting response from Mock ajax.

How to make sure that mock ajax response is available before calling assert statements in Qunit

4

1 回答 1

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

以及QUnit 中 Async 控制的一些文档。

于 2013-04-24T22:08:13.227 回答