Search code examples
javascriptjqueryajaxunit-testingqunit

QUnit with Ajax, QUnit passes the failing tests


I am looking into QUnit for JavaScript unit testing. I am in a strange situation where I am checking against the value returned from the Ajax call.

For the following test I am purposely trying to fail it.

// test to check if the persons are returned! 
test("getPersons", function() {
  getPersons(function(response) {
    // persons = $.evalJSON(response.d);
    equals("boo", "Foo", "The name is valid");
  });
});

But it ends up passing all the time. Here is the getPersons method that make the Ajax call.

function getPersons(callback) {
  var persons = null;

  $.ajax({
    type: "POST",
    dataType: "json",
    data: {},
    contentType: "application/json",
    url: "AjaxService.asmx/GetPersons",
    success: function(response) {
      callback(response);
    }
  });
}

Solution

  • Starting and stopping using the QUnit library seems to be working!

    // test to check if the persons are returned!
    test("getPersons", function() {
      stop();
      getPersons(function(response) {
        persons = $.evalJSON(response.d);
        equals(persons[0].FirstName, "Mohammad");
        start();
      });
    });