Próbuję przetestować przechwytywacz odpowiedzi, ale mam problem z wyłudzeniem obiektu $ window. Oto mój kod przechwytujących:Przechwytywacz odpowiedzi testu z jaśminem kątowym
'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);
};
});
A oto moja specyfikacja:
'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();
}));
});
});
});
});
Mam a: Expected 'http://localhost:8080/context.html' to equal 'http://accounts.domain.com/oauth/.....'
. Każda pomoc, jak kpić poprawnie obiekt okna $ lub bardziej ogólnie, jak przetestować przypadku przekierowania 401 +?
Wielkie dzięki za odpowiedź. Pomogło mi to zrozumieć niektóre rzeczy, o których nie wiedziałem. Jednak nadal mam ten sam błąd "Oczekiwany" http: // localhost: 8080/context.html "być" http: //dev-accounts.domain.dev: 3000/oauth/authorize ... ". Myślę, że jest to związane z '$ window.location.href', które jakoś zawsze zwracają' http: // localhost: 8080/context.html'. Jakieś pomysły? – lkartono
jakiej przeglądarki używasz testów? – user2943490
Używam PhantomJs – lkartono