Sometimes, we want to stub a class method with Sinon.js and JavaScript.
In this article, we’ll look at how to stub a class method with Sinon.js and JavaScript.
How to stub a class method with Sinon.js and JavaScript?
To stub a class method with Sinon.js and JavaScript, we can call the sinon.stub
method.
For instance, we write
sinon.stub(YourClass.prototype, "myMethod").callsFake(() => {
return {};
});
to stub the myMethod
instance method in the YourClass
class.
We call callsFake
with a callback that returns the value we want for the fake method.
Likewise, we stub a static method with
sinon.stub(YourClass, "myStaticMethod").callsFake(() => {
return {};
});
We call stub
to stub the YouClass.myStaticMethod
static method.
Conclusion
To stub a class method with Sinon.js and JavaScript, we can call the sinon.stub
method.