我有以下服务:
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
),堆栈跟踪只指向自己的角度源,所以它没有多大帮助.
发布于 2014-01-02 15:28:23
在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');
});
});
发布于 2014-04-13 09:40:11
我将提出一个更简单但可能不那么优雅的解决方案。我正在为$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。
发布于 2014-01-31 23:15:44
我会提供另一种可能对你有用的方法。在单元测试控制器“动作”时,我也遇到了同样的问题,它最终会重定向用户(满页加载,但在更大的网站/应用程序中转到不同的页面)。为了提供一些上下文,控制器发出一个AJAX请求,如果响应正常,它将通过$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()函数中添加了以下内容,以模拟$window服务:
mockWindow = { location: { replace: function(url) { console.log('redirecting to: ' + url); } } };
eventCtrl = $controller('EventCtrl', { $scope: scope, $window: mockWindow });
当然,这个解决方案阻止了我(干净地)验证替换函数是用一个预期的参数调用的,但是我现在并不关心这个问题.希望这能有所帮助。
https://stackoverflow.com/questions/20252382
复制相似问题