spyOn.and.callFake 在 Sinon 中相当于什么?

What is equivalent of spyOn.and.callFake in Sinon?

如何在Sinon中写一个调用Fake的spy,类似于Jasmine?

茉莉花:

spyOn(window, "requestAnimationFrame").and.callFake(() => {});

诗乃:

// pseudo code
const requestAnimationFrameSpy = spy().and.callFake(() => {}); 
global.window.requestAnimationFrame = requestAnimationFrameSpy;

您可以通过几种不同的方式执行此操作,其中一种是 sinon fakes 类似于:

const requestAnimationFrameSpy = sinon.fake().returns({value:'some value'}); 
global.window.requestAnimationFrame = requestAnimationFrameSpy();

您也可以使用 sinon stubs:

//from sinon website
var myObj = {};
myObj.prop = function propFn() {
    return 'foo';
};

sinon.stub(myObj, 'prop').callsFake(function fakeFn() {
    return 'bar';
});

myObj.prop(); // 'bar'