[英]How to test a function using jasmine-node which internally calls a function which returns a 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*****************");
})
});
//測試文件結束
現在,您可以看到我正在調用testFile.test()函數,它只是dataService.js中的測試函數。 此函數在dataService.js(相同文件)中調用getQuestions(),該函數返回一個Promise。 我在測試中嘲笑了getQuestions()函數,該函數被調用並正在解決諾言,但是我的test()成功方法沒有被調用,因此我的測試失敗了。
您的getQuestions
方法永遠無法解決承諾。
您有一個從0
到9
的循環,但只有在i === 10
才能解決。
更改:
for(i=0; i<10; i++) {
if(i===10) {
deferred.resolve(i);
}
}
至:
deferred.resolve(10);
作為一個一般性的技巧,調用返回promise的函數的方法應該自己返回promise,這樣您就可以輕松地測試它們並掛鈎其完成。 因此,我會讓.test
返回一個.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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.