栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

角茉莉测试响应拦截器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

角茉莉测试响应拦截器

您应该使用最新的语法来构造拦截器定义。您的URL构造也应包含在服务中,以便可以在测试中轻松模拟它。

.factory('UnauthorizedInterceptor', function($q, $window, OtherService) {  var service = {    responseError: handleUnauthorized  };  return service;  function handleUnauthorized(rejection) {    if (rejection.status === 401) {      $window.location.href = OtherService.getUnauthorizedRedirectURL();    }    return $q.reject(rejection);  }});

这样做可以让您像其他任何工厂一样对其进行测试,而不必担心

$http
拦截器的内部实现,也不必使用来模拟响应
$httpBackend

describe('Domain.handlers.response', function() {  var $window,      UnauthorizedInterceptor,      OtherService,      redirectUrl = 'someUrl';  beforeEach(module('Domain.handlers'));  beforeEach(function () {    $window = { location: { href: null } };    module(function($provide) {      $provide.value('$window', $window);    });  });  beforeEach(inject(function(_UnauthorizedInterceptor_, _OtherService_) {    UnauthorizedInterceptor = _UnauthorizedInterceptor_;    OtherService = _OtherService_;    spyOn(OtherService, 'getUnauthorizedRedirectURL').andReturn(redirectUrl);  }));  describe('UnauthorizedInterceptor', function() {    it('should be defined', function() {      expect(UnauthorizedInterceptor).toBeDefined();    });    it('should have a handler for responseError', function () {      expect(angular.isFunction(UnauthorizedInterceptor.responseError)).toBe(true);    });    describe('when HTTP 401', function () {      beforeEach(function () {        var rejection = { status: 401 };        UnauthorizedInterceptor.responseError(rejection);      });      it('should set window location', function () {        expect($window.location.href).toBe(redirectUrl);      });    });    describe('when not HTTP 401', function () {      beforeEach(function () {        var rejection = { status: 500 };        UnauthorizedInterceptor.responseError(rejection);      });      it('should not set window location', function () {        expect($window.location.href).not.toBe(redirectUrl);      });    });  });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/385443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号