I'm trying stub class constructors with Sinon.
The stub of the regular method 'omg' works fine, but the stub of the constructor fails the test, and the 'real' constructor is called instead of the stub.
Any ideas on what syntax I need to stub this correctly?
class Foo {
constructor() {
this.bar = new Bar();
}
omg() {
this.bar.omg();
}
}
class Bar {
constructor() {
console.log('In bar constructor');
}
omg() {
console.log('In bar omg');
}
}
const sandbox = sinon.createSandbox();
sandbox.stub(Bar.prototype, 'constructor');
sandbox.stub(Bar.prototype, 'omg');
describe('Foo', () => {
describe('Constructor', () => {
it('Should instantiate bar', () => {
const foo = new Foo();
expect(Bar.prototype.constructor.called).to.be.true;
});
});
describe('Omg', () => {
it("Should call Bar's omg method', () => {
const foo = new Foo();
foo.omg();
expect(Bar.prototype.omg.called).to.be.true;
});
});
});