dimanche 24 janvier 2016

Bypass Ajax request within javascript promise in Unit Testing

I have a function called getStudentData(),returns resolved data.

Inside getStudentData(), I have an Ajax request.

I want to Bypass Ajax request in my unit test case using Mocha , so that when i make a call to getStudentData(), the data should be returned.

Please find the code below:

getStudentData: function() {

        return studentData || (studentData = new Promise(function(resolve, reject) {
            var request = {
                //request data goes here
            };
            var url = "/student";
            $.ajax({
                url: url,
                type: "POST",
                data: JSON.stringify(request),
                dataType: "json",
                contentType: "application/json",
                success: function(response, status, transport) {
                   //success data goes here
                },
                error: function(status, textStatus, errorThrown) {
                    reject(status);
                }
            });
        }).then(function(data) {
            return data;
        })['catch'](function(error) {
           throw error;
        }));
    }

Please let me know how to Bypass Ajax request By stubbing data using sinon.js .so that when i make a call to getStudentData() , data should be returned.

Aucun commentaire:

Enregistrer un commentaire