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 prototyp开发者_运维技巧e.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:
spyOn(so, 'foo');
Or, if you insist that you want to verify that the prototype is called:
spyOn(SomeObject.prototype, 'foo');
Jasmine tears down all spies after each spec.
delete SomeObject.prototype.foo;
in tearDown or just in the end of concrete test.
精彩评论