Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spying on immutable native methods

I want to test if window.location.assign is being called, and so am trying to use spyOn(window.location, 'assign');, but the method isn't overwriteable.

Are there any other approaches I can use to spy on native methods that can't be overwritten?

like image 850
wheresrhys Avatar asked Nov 13 '22 01:11

wheresrhys


1 Answers

What you can do is to create a wrapper of the immutable function in your class:

MyClass.prototype.locationAssign = function () {
    window.location.assign.apply(window.location, arguments);
}

and spy on that method.

spyOn(MyClass, 'locationAssign');
like image 146
Kaizo Avatar answered Nov 14 '22 23:11

Kaizo