Spy on immutable native methods

I want to check if window.location.assign is being called, and so I'm trying to use spyOn(window.location, 'assign'); but the method is not overwritten.

Are there any other approaches that I can use to find my own methods that cannot be overwritten?

+4
source share
1 answer

What you can do is create an immutable function wrapper in your class:

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

and spy on this method.

 spyOn(MyClass, 'locationAssign'); 
+1
source

Source: https://habr.com/ru/post/1492938/


All Articles