To test a certain part of my code I need to mock out a prototype method to make sure it’s called. The code looks something like this.
SomeObject.prototype.foo = jasmine.createSpy()
var so = SomeObject()
so.bar()
expect(SomeObject.prototype.foo).toHaveBeenCalled()
This works but it changes the state of the SomeObject which I don’t want. So I was wondering is there a way to revert the state of prototype.foo after I am done? I was thinking maybe about making a copy of SomeObject. I took a look at jQuery.extend but I am not sure if I can use that to copy constructor functions.
Two choices. It’s preferable to spy on the instance:
Or, if you insist that you want to verify that the prototype is called:
Jasmine tears down all spies after each spec.