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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -