Javascript: Mocking Constructor using Sinon

I needed a solution for this because my code was calling the new operator. I wanted to mock the object that the new call created.

var MockExample = sinon.stub();
MockExample.prototype.test = sinon.stub().returns("42");
var example = new MockExample();
console.log("example: " + example.test()); // outputs 42

Then I used rewire to inject it into the code that I was testing

rewiredModule = rewire('/path/to/module.js');
rewiredModule.__set__("Example", example);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)