【发布时间】:2014-04-12 08:37:39
【问题描述】:
我对诗乃的用法还很陌生。
假设我们有模块(名为 myModule.js)定义:
//myModule.js
var _f2 = function() {
console.log('_f2 enter');
return {prop1:'var1'};
};
var f1 = function(){
var myVar1 = _f2();
console.log('_f2 called');
};
module.exports._f2 = _f2;
module.exports.f1 = f1;
这是对模块的测试
var sinon = require('sinon');
var myModule = require('./myModule');
describe('test my module',function(){
var f2Spy ;
beforeEach(function(){
f2Spy = sinon.spy(myModule,'_f2');
});
afterEach(function(){
myModule._f2.restore();
});
it('call _f2',function(done){
myModule.f1();
sinon.assert.called(f2Spy);
done();
})
});
运行此测试时,我收到断言错误,即未调用 _f2:
AssertError: expected _f2 to have been called at least once but was never called
但从日志消息中我可以看到调用了 _f2。 问题是:错误的原因是什么?在此先感谢
【问题讨论】:
标签: javascript node.js unit-testing sinon