mercredi 7 janvier 2015

How to test a function using jasmine-node which internally calls a function which returns a promise

I am just trying out jasmine-node. I need some help with promise resolution. I have simple js file



//dataService.js

var Q = require('q');
console.info("Q is "+Q);
exports.test = function() {
console.warn("Will call promise now");
this.getQuestions().then(function() {
console.log("Test..");
});
};

exports.getQuestions = function() {

var deferred = Q.defer();
for(i=0; i<10; i++) {
if(i===10) {
deferred.resolve(i);
}
}
return deferred.promise;
// return {
// 'Question1': 'What is your name'
// }
}

//end of dataService.js



And the test is

// testspec.js


var assert = require("assert");
var q = require('q');
var testFile = require('../routes/dataService');
var fs = require('fs');


describe('#indexOf()', function(done){
it('should return -1 when the value is not present', function(done){
console.log("Teststststst" + fs);
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
spyOn(testFile, 'getQuestions').andCallFake(function() {
console.warn("Spy Called********************");
var deferred = q.defer();
deferred.resolve(1);
console.info("passing 1****");
//done(1);
return deferred.promise;
});
spyOn(console, 'log');
testFile.test();
console.info("Testststststsinggggggggg");
expect(console.log).toHaveBeenCalledWith("Test..");
console.info("Done*****************");
})
});


//end of test file


Now as you can see I am calling testFile.test() function which is nothing but the test function in dataService.js. This function calls the getQuestions() in the dataService.js(same file), which returns a promise. I have mocked the getQuestions() function in my test, it is getting called and is resolving the prmise, but my test() success method is not getting called, so my test is failing.


Pls help me with the test. Any help ASAP is appreciated.


Thanx in advance. I


Aucun commentaire:

Enregistrer un commentaire