[英]How to mock variable with Sinon/Mocha in node.js
这是我的代码:start_end.js
var glb_obj, test={};
var timer = 10;
test.start_pool = function(cb) {
timer--;
if(timer < 1) {
glb_obj = {"close": true}; // setting object
cb(null, "hello world");
} else {
start_pool(cb);
}
}
test.end_pool = function(){
if(glb_obj && glb_obj.close) {
console.log("closed");
}
}
module.exports = test;
测试用例:
var sinon = require('sinon');
var start_end = require('./start_end');
describe("start_end", function(){
before(function () {
cb_spy = sinon.spy();
});
afterEach(function () {
cb_spy.reset();
});
it("start_pool()", function(done){
// how to make timer variable < 1, so that if(timer < 1) will meet
start_end.start_pool(cb_spy);
sinon.assert.calledWith(cb_spy, null, "hello world");
});
});
如何改变可变timer
和glb_obj
使用兴农的职权范围内?
从v4.1.2
使用Sinon
是v4.1.2
。
Sinon
专注于通过存根和嘲笑来测试行为-而不是更改内部状态。
如果要更改私有变量的值,请考虑使用类似rewire
:
如果有人还想用Sinon存根,我们可以存根测试对象的getters方法和mock get方法,对我来说效果很好
sinon.stub(myObj, 'propertyName').get(() => 'mockedValue');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.