0

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?

Vu Le Anh
  • 708
  • 2
  • 8
  • 21
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Daniel B Jan 12 '17 at 10:04
  • Possible duplicate of [Testing for a function that contains asynchronous code](http://stackoverflow.com/questions/15673578/testing-for-a-function-that-contains-asynchronous-code) – Jordan Kasper Jan 13 '17 at 17:39

0 Answers0