您如何测试此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
...
答案 0 :(得分:1)
嘿,自从你发布这个问题已经很久了,但我认为我有这个问题的解决方案。最后描述是针对intreceptors中的requestError方法。这是我如何测试我的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);
});
});
});
})();
实际拦截器
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);
}
};
}