Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想测试是否window.location.assign被调用,所以我试图使用spyOn(window.location, 'assign');,但该方法不可覆盖。
window.location.assign
spyOn(window.location, 'assign');
还有其他方法可以用来监视无法覆盖的本机方法吗?
您可以做的是在您的类中创建一个不可变函数的包装器:
MyClass.prototype.locationAssign = function () { window.location.assign.apply(window.location, arguments); }
并监视该方法。
spyOn(MyClass, 'locationAssign');