Saturday, August 18, 2018

Jasmine real AJAX request test

I need to make a test with a real ajax request with jasmine,

So far I have this:

function login(){
    $.ajax({
        method: 'GET',
        url: url,
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    });
}

I need to get the login success response, but for that, jasmine needs to wait for the request to end before iteration trough the 'its'. How can I do that?

Solved

You need to use "done()" and then call it once your async request has been completed. It is a part of the jasmine testing suite. So something like this.

function(done){
    $.ajax({
        method: 'GET',
        url: url,
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    });
    done();
}

Shouldn't it be more like

function(done){
 $.ajax({
    method: 'GET',
    url: url,
    dataType: "json",
    contentType: "application/json; charset=utf-8"
 })
 .then(done);
}

or replace done with a method that actually checks

.then(function(response){
   //do your asserts here
   done();
});

No comments:

Post a Comment