Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了anglejs – 角茉莉花测试响应拦截器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图测试我的响应拦截器,但是我很难弄清楚如何模拟$window对象.这是我的拦截代码
'use Strict';

angular.module('Domain.handlers')

.config(function($httpProvider) {
  $httpProvider.responseInterceptors.push('UnauthorizedInterceptor');
})

.factory('UnauthorizedInterceptor',function($q,$injector,$window,ENV) {
  return function(promisE) {
    var success = function(responsE) { return response; };
    var error   = function(responsE) {
      if (response.status === 401) {
        $window.LOCATIOn.href = ENV.account + '/oauth/authorize?client_id=' + ENV.clientId + '&redirect_uri=' + ENV.app + '/oauth/callBACk&response_type=token';
      }
      return $q.reject(responsE);
    };
    return promise.then(success,error);
  };
});

这里是我的规格:

'use Strict';

describe('Domain.handlers.response',function() {
  var UnauthorizedInterceptor,httpProvider,$httpBACkend,$http,token = '123456789';

  beforeEach(module('Domain.handlers',function($httpProvider) {
    httpProvider = $httpProvider;
  }));

  beforeEach(inject(function(_UnauthorizedInterceptor_,_$httpBACkend_,_$http_) {
    UnauthorizedInterceptor = _UnauthorizedInterceptor_;
    $httpBACkend = _$httpBACkend_;
    $http = _$http_;
  }));

  describe('UnauthorizedInterceptor',function() {
    it('should be defined',function() {
      expect(UnauthorizedInterceptor).toBeDefined();
    });

    describe('http status',function() {
      describe('is 200 OK',function() {
        it('should return a 200 status',function() {
          $httpBACkend.expectGET('http://api.domain.com/clients').respond(200,{});
          $http.get('http://api.domain.com/clients');
          $httpBACkend.flush();
        });
      });

      describe('is 401 Unauthorized',function() {
        it('should redirect to accounts.domain.com',inject(function($window) {
          $httpBACkend.expectGET('http://api.domain.com/clients').respond(401,{});
          $http.get('http://api.domain.com/clients');
          expect($window.LOCATIOn.href).toEqual('http://accounts.domain.com/oauth/.....');
          $httpBACkend.flush();
        }));
      });
    });
  });
});

我有一个:预期’http:// localhost:8080 / context.html’等于’http://accounts.domain.com/oauth / …..’.任何帮助如何正确地模拟$window对象或更一般如何测试401重定向的情况?

您应该使用 more recent syntax构建拦截器定义.您的URL构造也应该在一个服务中,以便容易地在测试中被嘲笑.
.factory('UnauthorizedInterceptor',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() {
      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);
      });
    });
  });
});

大佬总结

以上是大佬教程为你收集整理的anglejs – 角茉莉花测试响应拦截器全部内容,希望文章能够帮你解决anglejs – 角茉莉花测试响应拦截器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。