Mocha JS 测试 - 一种对 sinon spy 进行回调的方法
Mocha JS tests - a way to put a callback on sinon spy
我想实现这样的目标:
it('should do something', function(done) {
var func_spy = sinon.spy(obj, 'method');
func_spy.on('called', function() {
// check something
done();
});
}
这意味着我想在我的间谍上设置一个回调,在调用包装函数时调用(或者在它 returns 时更好)。 sinon 或任何其他库有可能吗?
谢谢
您可以使用 sinon 存根和回调调用原始方法。参见 here
var originalMethod = obj['method']
sinon.stub(obj,'method',function() {
originalMethod();
//TODO what ever you need
});
我想实现这样的目标:
it('should do something', function(done) {
var func_spy = sinon.spy(obj, 'method');
func_spy.on('called', function() {
// check something
done();
});
}
这意味着我想在我的间谍上设置一个回调,在调用包装函数时调用(或者在它 returns 时更好)。 sinon 或任何其他库有可能吗?
谢谢
您可以使用 sinon 存根和回调调用原始方法。参见 here
var originalMethod = obj['method']
sinon.stub(obj,'method',function() {
originalMethod();
//TODO what ever you need
});