以下“设置”方法需要使用sinon进行测试,我不确定该怎么做。
// foo is just a wrapper
function Foo() {
this.bar = new Bar();
}
Foo.prototype.set = function(x) {
this.bar.set(x);
}
这里是对它进行单元测试的尝试:
var foo = new Foo();
it("can called set method", function() {
foo.set(x);
foo.bar.set.calledOnceWith(x);
});
foo.bar.set.knownOnceWith不是函数。
答案 0 :(得分:1)
您已经关闭。
您只需要在spy
上创建Bar.prototype.set
:
import * as sinon from 'sinon';
function Bar() { }
Bar.prototype.set = function(x) {
console.log(`Bar.prototype.set() called with ${x}`);
}
function Foo() {
this.bar = new Bar();
}
Foo.prototype.set = function(x) {
this.bar.set(x);
}
it('calls set on its instance of Bar', () => {
const spy = sinon.spy(Bar.prototype, 'set'); // spy on Bar.prototype.set
const foo = new Foo();
foo.set(5);
sinon.assert.calledWithExactly(spy, 5); // SUCCESS
})