如何使用jasmine-nodetesting函数,内部调用返回promise的函数?

我只是尝试了茉莉节点。 我需要一些有关承诺解决的帮助。 我有简单的js文件

//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*****************"); }) }); 

//testing文件结束

现在你可以看到我正在调用testFile.test()函数,它只是dataService.js中的testing函数。 这个函数调用dataService.js(同一个文件)中的getQuestions(),它返回一个promise。 我在我的testing中嘲笑getQuestions()函数,它正在调用并解决承诺,但我的test()成功方法没有被调用,所以我的testing失败。

您的getQuestions方法永远不会解决承诺。

你有一个循环运行从09但你只解决它,如果i === 10

更改:

 for(i=0; i<10; i++) { if(i===10) { deferred.resolve(i); } } 

至:

 deferred.resolve(10); 

作为一个通用的提示方法,调用返回promise的函数应该自己返回promise,这样你可以很容易地testing它们并且完成它们。 出于这个原因,我会使.test返回一个承诺(而不是只是叫它)

 I was able to run the test by returning a promise from the test() function. //dataService.js var Q = require('q'); console.info("Q is "+Q); exports.test = function() { console.warn("Will call promise now"); return this.getQuestions().then(function() { console.log("Test.."); return 'success'; }); }; exports.getQuestions = function() { var deferred = Q.defer(); for(i=0; i<10; i++) { if(i===3) { deferred.resolve(i); } } return deferred.promise; // return { // 'Question1': 'What is your name' // } } //end of dataService.js //dataServicespec.js var assert = require("assert"); var q = require('q'); var testFile = require('../routes/dataService');//include the dataService.js var fs = require('fs'); describe('Tests', function(done) { it('should run the test properly', function(done) { console.log("Teststststst" + fs); var flag = false; var deferred; spyOn(testFile, 'getQuestions').andCallFake(function() { console.warn("Spy Called********************"); deferred = q.defer(); console.info("passing 1****"); deferred.resolve(1); return deferred.promise; }); spyOn(console, 'log'); runs(function() { var p = testFile.test(); p.then(function() { flag = true; }); }); waitsFor(function() { if (flag === true) return true; }); runs(function() { console.info("Testststststsinggggggggg"); expect(console.log).toHaveBeenCalledWith("Test.."); console.info("Done*****************"); done(); }); }) }); //end of dataServicespec.js Thanx @Benjamin on you suggestion for returning a promise from test.