我有以下服务:
angular.module("services")
.factory("whatever", function($window) {
return {
redirect: function() {
$window.location.replace("http://www.whatever.com");
}
};
});
如何嘲笑在单元测试$window
对象,以防止运行测试时重新加载页面?
我尝试使用
spyOn($window.location, 'replace').andReturn(true);
,但它没有工作(仍然有"Some of your tests did a full page reload!"
误差)和
$provide.value('$window', {location: {replace: jasmine.createSpy()}})
,但我得到一个错误(Error: [ng:areq] Argument 'fn' is not a function, got Object
)与堆栈跟踪只指向有角度的自己的源,所以它不是非常有用...
我一直面临同样的问题。你有没有想出一个解决方案? – Tushar
PaulL提供的将'$ window.location'封装在单独服务中的解决方案实际上工作得很好。还没有尝试过LostInComputer的解决方案。 – szimek