本文介绍了单元测试AngularJS $窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想单元测试以下AngularJs服务:
.factory('HTT presponseInterceptor',['$ Q','$的位置,$窗口','CONTEXT_PATH',函数($ Q $位置,$窗口的contextPath){
返回{
回应:功能(响应){
//将只要求HTTP高达300
返回响应;
},
responseError:函数(拒绝){
如果(rejection.status === 405 || rejection.status === 401){
$ window.location.href =的contextPath +'/登入';
}
返回$ q.reject(拒绝);
}
};
}]);
我曾尝试用下面的套件:
描述('控制器',函数(){
变量$范围,CTRL;
beforeEach(模块('curriculumModule'));
beforeEach(模块('curriculumControllerModule'));
beforeEach(模块('curriculumServiceModule'));
beforeEach(模块(函数($提供){
$ provide.constant('CONTEXT_PATH','bignibou'); //这里覆盖的contextPath
}));
描述(CreateCurriculumCtrl',函数(){
VAR mockBackend,位置,_window;
beforeEach(注(函数($ rootScope,$控制器,$ httpBackend,$位置$窗口){
mockBackend = $ httpBackend;
位置= $位置;
_window = $窗口;
$范围= $ rootScope $新的()。
CTRL = $控制器('CreateCurriculumCtrl',{
$范围:$范围
});
})); 它('应该重定向到/登入如果401或405',函数(){
mockBackend.whenGET('bignibou/utils/findLanguagesByLanguageStartingWith.json?language=fran').respond([{\"description\":\"Français\",\"id\":46,\"version\":0}]);
mockBackend.whenPOST('bignibou /课程/新)。反应(功能(方法,URL,数据报头){
返回[401];
});
$ scope.saveCurriculum();
mockBackend.flush();
期待(_window.location.href).toEqual(/ bignibou /登入);
});
});
});
然而,它失败,出现以下错误信息:
PhantomJS 1.9.2(Linux)的控制器CreateCurriculumCtrl应该重定向到/登入如果401或405失败
预期的http://本地主机:9876 / context.html'等于'/ bignibou /登入。
PhantomJS 1.9.2(Linux)的错误
你的一些测试做了全面的重新加载页面!
我不知道什么错误,为什么。任何人都可以请帮助?
我只是想确保 $ window.location.href
等于 / bignibou /登入
编辑1
我设法得到它的工作方式如下(感谢dskh):
beforeEach(模块('配置',函数($提供){
$ provide.value('$窗口',{位置:{HREF:'假'}});
}));
解决方案
当您在模块中加载你可以注入模拟的依赖关系:
angular.mock.module('curriculumModule',函数($提供){
$ provide.value('$窗口',{位置:{HREF:'假'}});
});
I would like to unit test the following AngularJs service:
.factory('httpResponseInterceptor', ['$q', '$location', '$window', 'CONTEXT_PATH', function($q, $location, $window, contextPath){
return {
response : function (response) {
//Will only be called for HTTP up to 300
return response;
},
responseError: function (rejection) {
if(rejection.status === 405 || rejection.status === 401) {
$window.location.href = contextPath + '/signin';
}
return $q.reject(rejection);
}
};
}]);
I have tried with the following suite:
describe('Controllers', function () {
var $scope, ctrl;
beforeEach(module('curriculumModule'));
beforeEach(module('curriculumControllerModule'));
beforeEach(module('curriculumServiceModule'));
beforeEach(module(function($provide) {
$provide.constant('CONTEXT_PATH', 'bignibou'); // override contextPath here
}));
describe('CreateCurriculumCtrl', function () {
var mockBackend, location, _window;
beforeEach(inject(function ($rootScope, $controller, $httpBackend, $location, $window) {
mockBackend = $httpBackend;
location = $location;
_window = $window;
$scope = $rootScope.$new();
ctrl = $controller('CreateCurriculumCtrl', {
$scope: $scope
});
}));
it('should redirect to /signin if 401 or 405', function () {
mockBackend.whenGET('bignibou/utils/findLanguagesByLanguageStartingWith.json?language=fran').respond([{"description":"Français","id":46,"version":0}]);
mockBackend.whenPOST('bignibou/curriculum/new').respond(function(method, url, data, headers){
return [401];
});
$scope.saveCurriculum();
mockBackend.flush();
expect(_window.location.href).toEqual("/bignibou/signin");
});
});
});
However, it fails with the following error message:
PhantomJS 1.9.2 (Linux) Controllers CreateCurriculumCtrl should redirect to /signin if 401 or 405 FAILED
Expected 'http://localhost:9876/context.html' to equal '/bignibou/signin'.
PhantomJS 1.9.2 (Linux) ERROR
Some of your tests did a full page reload!
I am not sure what is going wrong and why. Can anyone please help?
I just want to ensure the $window.location.href
is equal to '/bignibou/signin'
.
edit 1:
I managed to get it to work as follows (thanks to "dskh"):
beforeEach(module('config', function($provide){
$provide.value('$window', {location:{href:'dummy'}});
}));
解决方案
You can inject mock dependencies when you load in your module:
angular.mock.module('curriculumModule', function($provide){
$provide.value('$window', {location:{href:'dummy'}});
});
这篇关于单元测试AngularJS $窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!