如何在AngularJS单元测试中模拟$window.location.replace?

11 浏览
0 Comments

如何在AngularJS单元测试中模拟$window.location.replace?

我有以下服务:

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),而堆栈跟踪只指向angular自身的源代码,所以没有什么帮助...

0