John Abraham
John Abraham

Reputation: 18791

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

How do I write a spy that callsFake in Sinon, similar to Jasmine?

Jasmine:

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

Sinon:

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

Upvotes: 2

Views: 1830

Answers (1)

BlackICE
BlackICE

Reputation: 8926

You can do this a couple different ways, either with sinon fakes similar to:

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

You can also do this with 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'

Upvotes: 3

Related Questions