I need to make sure that a certain method inside the below shown UserMock-class was called. I’ve created this mock version to inject into another module to prevent default behaviour during testing.
I am already using sinon.js, so how can I access a method such as isValid() and replace it with a spy/stub? Is it possible to do this without instantiating the class?
var UserMock = (function() {
var User;
User = function() {};
User.prototype.isValid = function() {};
return User;
})();
Thank you
Simply via
prototype:Explanation:
and
Makes a reference to the method
isValueto the variable_old. The closure is made so we don’t pulede the parent scope with the variable.Redeclares the prototype method
Calling the old method and returning the result from it.
Using apply lets put in the right scope (
this) with all the arguments passed to the functionEg. if we make a simple function and apply it.