2013-11-27 39 views
39

我有以下服务:

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)与堆栈跟踪只指向有角度的自己的源,所以它不是非常有用...

+0

我一直面临同样的问题。你有没有想出一个解决方案? – Tushar

+0

PaulL提供的将'$ window.location'封装在单独服务中的解决方案实际上工作得很好。还没有尝试过LostInComputer的解决方案。 – szimek

回答

54

在Chrome中(没有测试其他浏览器),location.replace是只读的,所以spyOn无法取代它。

$provide.value应该工作。代码中的某处肯定是错的。

这里是一个工作单元测试

describe('whatever', function() { 
    var $window, whatever; 

    beforeEach(module('services')); 

    beforeEach(function() { 
     $window = {location: { replace: jasmine.createSpy()} }; 

     module(function($provide) { 
     $provide.value('$window', $window); 
     }); 

     inject(function($injector) { 
     whatever = $injector.get('whatever'); 
     }); 
    }); 

    it('replace redirects to http://www.whatever.com', function() { 
     whatever.redirect(); 
     expect($window.location.replace).toHaveBeenCalledWith('http://www.whatever.com'); 
    }); 
}); 
+4

Angular仍然向服务注入实际窗口对象 – Tushar

+0

请向我们显示您的代码 – LostInComputer

+0

这里是[工作代码](https://dl.dropboxusercontent.com/u/14576915/window_test.zip),演示我发布的答案。要运行,1.你必须有NodeJS 2.调用'npm install'3.调用'grunt test'来运行测试 – LostInComputer

0

我想你想要的是使用$location服务,而不是调用$window.location。这里还有一整个页面解释这个功能:http://docs.angularjs.org/guide/dev_guide.services.$location

使用这个,在测试中使用$ location服务的stubbed版本应该相当简单。

+2

谢谢,但根据文档:“当您需要更改URL并重新加载页面或导航到其他页面时,请使用较低级别的API,$ window.location.href。”这正是我想要的 - 重定向到外部URL。 – szimek

+0

啊,我明白了。我不认为,这是可能的,但我可以想象如何工作的唯一方法就是使用自己的window.location对象来存储。 – calamari

4

我会提供了另一种方法可能会为你工作。我在单元测试最终重定向用户的控制器“操作”(整页加载,但到更大的网站/应用程序中的不同页面)时面临同样的问题。给予一定的情况下,控制器打完一个AJAX请求,如果响应是OK,它将通过$ window.location.replace()用户在不同的页面重定向:

$http.post('save', data) 
     .success(function(responseData, status, headers, config) { 
      if(responseData.redirect) { 
       $window.location.replace(responseData.redirect); 
      } 
     }) 
     .error(function(responseData, status, headers, config) { 
      console.error("ERROR while trying to create the Event!!"); 
     }); 

测试此控制器功能导致相同的“你的一些测试做了整页重新加载!”错误。所以我增加了以下为控制器规范的beforeEach()函数,来模拟出$窗口服务:

mockWindow = { location: { replace: function(url) { console.log('redirecting to: ' + url); } } }; 
eventCtrl = $controller('EventCtrl', { $scope: scope, $window: mockWindow }); 

当然,这种解决方案阻止我(干净)验证的替代函数被调用一个预期的论点,但我现在不太在意这个......希望有所帮助。

+1

这将适用于控制器,但如果要将$ window注入到服务中则会失败。 – Tushar

6

我正在做一个更简单但也许不那么优雅的解决方案。我正在为$ window.location写一个包装,然后我可以模拟它。将它与你的代码相关联,我会模仿whatever.redirect函数,而不是模拟$ window(我假设你的真实函数更复杂)。

所以我结束了:

angular.module("services") 
.factory("whatever", function($window) { 
    return { 
    do_stuff_that_redirects: function() { 
     lots of code; 
     this.redirect("http://www.whatever.com"); 
     maybe_more_code_maybe_not; 
    }, 

    redirect: function(url) { 
     $window.location.replace(url); 
    } 
    }; 
}); 

那么我就可以直接嘲笑重定向方法,只是相信,因为它只有一行代码就真的不能出差错。

spyOn(whatever, 'redirect').andCallFake(function(){}); 
expect(whatever.redirect).toHaveBeenCalledWith('http:/my.expected/url'); 

这对我的目的是足够的,并让我验证被调用的url。

0
$location.path('/someNewPath'); 

$ location.replace(); //或者你可以将它们链接为:$ location.path('/ someNewPath')。replace();

相关问题