調用私有函數單元測試模塊,我是新來的節點單元測試,撞上了路障,當涉及到承諾。我的模塊通過ApiController執行搜索,然後返回一個promise。根據其解析狀態,它會調用模塊中的兩個私有函數中的一個。是在承諾的決心
模塊:
module.exports.process = function(req, res) {
let searchTerm = req.query.searchValue;
ApiController.fetchResults(searchTerm)
.then((results) => onReturnedResults(results), (err) => onNoResults());
function onReturnedResults(results) {
req.app.set('searchResults', results);
res.redirect('/results');
}
function onNoResults() {
res.render('search/search', { noResultsFound: true });
}
};
測試:
var res = {
viewName: '', data : {},
render: function(view, viewData) {
this.view = view;
this.viewData = viewData;
}
};
var req = { query: { searchValue: 'doesnotexist' } }
describe('When searching for results that do not exist', function() {
it('should display a no results found message', function(done) {
let expectedResult = {
noResultsFound: true;
}
SearchController.process(req, res);
// Assert...
expect(res.viewData).to.be.equal(expectedResult);
done();
});
})
什麼是解決這個問題的最佳做法,我怎麼能「模擬」的fetchResults返回的承諾,這樣它實際上並沒有從取結果API(同時仍然調用私有函數)?
謝謝,這是真正的幫助:) – tctc91