我想在構造函數上創建一個間諜,並查看它是否被調用 - 下面是我的測試。 我正在使用sinon-chai,所以語法是有效的,但是兩個測試都失敗了。在sinon上的javascript中偵聽構造函數
var foo = function(arg) {
};
var bar = function(arg) {
var baz = new foo(arg);
};
it('foo initialized inside of this test', function() {
var spy = sinon.spy(foo);
new foo('test');
expect(spy).to.be.called;
expect(spy).to.be.calledWith('test');
});
it('foo initialized by bar()', function() {
var spy = sinon.spy(foo);
bar('test');
expect(spy).to.be.called;
expect(spy).to.be.calledWith('test');
});
類似問題+ Anwser:https://stackoverflow.com/questions/32338427/spying-on-date-constructor-with-sinon?rq=1 – Gyuri 2017-09-18 22:05:16