2015-01-07 33 views
1

我只是嘗試了茉莉節點。我需要一些有關承諾解決的幫助。我有簡單的JS文件如何使用jasmine-node測試函數,該函數內部調用返回promise的函數?

//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()成功方法沒有被調用,所以我的測試失敗了。

回答

2

您的getQuestions方法永遠不會解決承諾。

您有一個從09的循環,但只有在i === 10時才能解決。

變化:

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

要:

deferred.resolve(10); 

作爲一般的提示方法調用返回的承諾函數應返回承諾自己所以你可以伊斯利對其進行測試,並鉤住其完成。爲此,我會想辦法讓.test回報承諾(而不是僅僅把它)

+0

嘿,我<10爲i <= 10,這是一個錯字.....對不起..... ..測試不工作....實際上我已經窺探了getQuestions .....並且spied方法正在調用....但測試方法成功方法並未被調用 –

0
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.