javascript - How can you use Sinon (or something similar) to stub a method that's not on the relevant prototype? -
suppose create object using constructor:
function myconstructor() { this.mymethod = () => console.log('foo'); } const myobject = new myconstructor();
and in test file want stub mymethod
method. if had defined mymethod
on prototype, pretty straightforward. 1 use:
sinon.stub(myconstructor.prototype, 'mymethod')
but doesn't work when method defined in constructor using this
, in example above.
this issue that's hard avoid because 3rd-party libraries define instance methods in way, i.e., within constructor instead of on prototype. , need way stub such methods when testing.
Comments
Post a Comment