gpt4 book ai didi

angularjs - Angular 和 Jasmine : How to test requestError/rejection in HTTP interceptor?

转载 作者:可可西里 更新时间:2023-11-01 17:35:45 24 4
gpt4 key购买 nike

你会如何测试这个requestError方法?

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
...

最佳答案

嘿,你发布这个问题已经很久了,但我想我有这个问题的解决方案。最后描述的是拦截器中的 requestError 方法。这是我如何测试我的 httpInterceptor 的示例:

测试

/* 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);
});
});
});
})();

实际拦截器

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);
}
};
}

关于angularjs - Angular 和 Jasmine : How to test requestError/rejection in HTTP interceptor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32048903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com