mercredi 25 mars 2015

Mocking nested function returning Deferred in Jasmine

I'm trying to write a Jasmine test for a module that uses a jQuery ajax wrapper library named jquery.rest


This module under test:



var module = function() {

function getData() {
var IP = client.add('ip');
IP.read().done(function (data) {
console.log("done");
return data;
});
}

return {
getData: getData
}

}();


The client variable is declared in a different file, like so:



var client = new $.RestClient('/rest/api/');


I would like to mock the read() function so that it would return a Json payload that I define in my test. The read() method returns a $.Deferred object.


I have tried different approaches (using Jasmine spies) but without success.


Aucun commentaire:

Enregistrer un commentaire