Angular and Jasmine: How to test requestError / re

2019-08-10 20:30发布

问题:

How would you test this requestError method?

angular.module('myApp').factory 'HTTPInterceptor', [
  '$rootScope'
  '$q'
  '$window'
  'LocalStorageService'
  '$injector'
  ($rootScope, $q, $window, $injector) ->
    {

  request: (config) ->
    config.headers = config.headers or {}
    // do stuff with config then
    config # Return Config

  requestError: (rejection) ->
    q.reject(rejection) # Return the promise rejection
...

回答1:

Hey its been long since you post this question but I think I have solution for this question.Last describe is for requestError method in intreceptors. Here is example of how I test my httpInterceptor:

TEST

/* jshint undef:false*/
(function() {
    'use strict';

    describe('httpInterceptor', function() {
        var httpInterceptor, modal, state, q, actualOptions, rootScope, scope, mockAuthenticationService, notification;
        beforeEach(module('app'));
        beforeEach(inject(function(_httpInterceptor_, $q, $uibModal, $state, $rootScope, _AuthenticationService_, _Notification_) {
            httpInterceptor = _httpInterceptor_;
            rootScope = $rootScope;
            scope = rootScope.$new();
            q = $q;
            state = $state;
            mockAuthenticationService = _AuthenticationService_;
            notification = _Notification_;

            function FakeModal(){
                this.resultDeferred = $q.defer();
                this.result = this.resultDeferred.promise;
            }
            FakeModal.prototype.open = function(options){
                actualOptions = options;
                return this;  };
            FakeModal.prototype.close = function (item) {
                this.resultDeferred.resolve(item);
                rootScope.$apply(); // Propagate promise resolution to 'then' functions using $apply().
            };
            FakeModal.prototype.dismiss = function (item) {
                this.resultDeferred.reject(item);
                rootScope.$apply(); // Propagate promise resolution to 'then' functions using $apply().
            };
            modal = new FakeModal();
        }));

        describe('httpInterceptor', function() {
            beforeEach(function () {
                var rejection = { status: 400 , data: {exception: "class org.usda.fns.memsng.exceptions.ReviewTimeoutException"}};
                httpInterceptor.responseError(rejection);
            });
            it('with 400 error status and open idle modal', function () {

                rootScope.$apply();

                modal.close('close');
            });
        });
        describe('httpInterceptor', function() {
            it('with 400 error status and open notification', function () {
                var rejection = { status: 400 , data: {exception: "test"}};
                httpInterceptor.responseError(rejection);
            });
        });
        describe('httpInterceptor', function() {
            beforeEach(function () {
                spyOn(state, 'go');
                var rejection = { status: 403 };
                httpInterceptor.responseError(rejection);
            });
            it('with 403 error status and go to unauthorized page', function () {
                var expectedState = 'root.unauthorized';
                expect(state.go).toHaveBeenCalledWith(expectedState);
            });
        });
        describe('httpInterceptor', function() {
            beforeEach(function () {
                spyOn(state, 'go');

            });
            it('with requestError method', function () {
                var rejection = { status: 403 };
                httpInterceptor.requestError(rejection);
            });
        });
    });
})();

Actual Interceptor

function httpInterceptor($q, $log, $injector) {
        return {
            request: function(config) {
                return config;
            },
            requestError: function(rejection) {
                $log.debug(rejection);
                return $q.reject(rejection);
            },
            response: function(response) {
                $log.debug('response: ', response);
                return response;
            },
            responseError: function(rejection) {
                $log.debug(rejection);
                var state = $injector.get('$state');

                if (rejection.status === 403) {
                state.go('root.unauthorized');
               }
            return $q.reject(rejection);
        }
    };
}