Sometimes, we want to mock constructor using Sinon and JavaScript.
In this article, we’ll look at how to mock constructor using Sinon and JavaScript.
How to mock constructor using Sinon and JavaScript?
To mock constructor using Sinon and JavaScript, we call the sinon.stub method.
For instance, we write
const MockExample = sinon.stub();
MockExample.prototype.test = sinon.stub().returns("42");
const example = new MockExample();
console.log(example.test());
to create the MockExample constructor stub with stub.
Then we set its test instance method to return 42 with sinon.stub().returns("42");.
Then we create a MockExample instance and call its test method.
Conclusion
To mock constructor using Sinon and JavaScript, we call the sinon.stub method.