SCP
SCP

Reputation: 63

Qunit Assert mockajax response

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

Upvotes: 0

Views: 1111

Answers (1)

Jordan Kasper
Jordan Kasper

Reputation: 13273

You'll need to do an asyncTest versus a simple test call. Here's an example from one I use:

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

And some documentation of Async control in QUnit.

Upvotes: 1

Related Questions