0
我對mocha/chai/sinon以及一般測試非常陌生。我成功地測試了一個基本的express服務器,一個返回promise的函數,以及一個基本的sequelize設置來讓我的鼻子變溼;但我被困在間諜/存根/嘲笑。我該如何窺探與sinon的嵌套依賴關係
我的拳頭打嗝試圖檢查水珠被稱爲外部模塊:
//in utils.js
var glob = require('glob');
module.exports = {
funToTest: function (msg, callback) {
console.log(msg);
glob('*md', {
cwd: 'files/'
}, function (err, files) {
console.log(files);
});
callback();
callback();
}
};
使用摩卡/柴/興農/興農釵組合:
// in utils-test.js
var utils = require('utils.js');
var glob = require('glob');
describe('Utils', function() {
describe('funToTest method', function() {
const callback = sinon.spy();
const globSpy = sinon.spy(glob);
before(function (done) {
utils.funToTest('Files:', callback);
done();
});
// This PASSES fine
it ('should call our callback twice', function() {
expect(callback).to.have.been.calledTwice;
});
// This NOT SO MUCH
it('should call glob once', function() {
expect(globSpy).to.have.been.calledOnce;
});
)};
)};
以上斷言錯誤失敗:
AssertionError: expected glob to have been called exactly once, but it was called 0 times
那麼我該如何窺探utils.funToT中的glob依賴估計是否被調用?
感謝和以往任何指針...
您是否試過「expect(globspy.calledOnce).to.be.true」? –