The code:
var obj = {
check: 0,
foo: function() {
...
jQuery.ajax({
contentType : "application/json",
type: "POST",
async: true,
...,
success: function(data) {
check = 1;
},
error: function(error) {
}
});
}
}
The test code:
QUnit.test("Test asynchronus, function(assert) {
obj.foo();
assert.equal(obj.check, 1, "The check should be equal 1");
});
With current code, the test case is failed because the assert run before the callback success of ajax finish.
How to fix the test code without change the code (e.g: change async
to false
). assert.async
is solution for my problem?