我正在测试一个Http Interception服务,该服务在返回错误响应时使用路由导航到另一个URL,由于我未在测试路由本身,所以当前所有测试都通过了,但是控制台上显示了异常
Unhandled Promise rejection: Cannot match any routes. URL Segment: 'login' ; Zone: ProxyZone ; Task: Promise.then ; Value: Error: Cannot match any routes. URL Segment: 'login'
我曾尝试对我的routerByUrl函数上的路由器服务进行监视,但似乎没有被使用,因为我一直遇到相同的错误,我已经尝试过使用RouterTestingModule和Router Stubbing,但是看起来就像我的服务只是使用另一台路由器服务,而不是我的.spec提供的服务。
这是我的.spec代码(出于工作原因,我删除了网址):
import { TestBed } from '@angular/core/testing';
import { AuthInterceptorService } from './auth-interceptor.service';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpService } from '../http/http.service';
import { InternalDataService } from '../internal-data/internal-data.service';
import { Router } from '@angular/router';
describe('Service: AuthInterceptor', () => {
let service: HttpService;
let httpMock: HttpTestingController;
let data: InternalDataService;
let router = {
navigate: jasmine.createSpy('navigateByUrl')
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
],
providers: [
HttpService,
InternalDataService,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptorService,
multi: true
},
{
provide: Router,
useValue: router,
}
],
});
service = TestBed.get(HttpService);
httpMock = TestBed.get(HttpTestingController);
router.navigate.and.callFake(() => {});
});
it('deve adicionar o header de autenticação', () => {
data = TestBed.get(InternalDataService);
data.setAuthToken('Teste');
service.getProdutos().subscribe(res => {
expect(res).toBeTruthy();
})
const httpRequest = httpMock.expectOne('<doesnt matter>');
expect(httpRequest.request.headers.has('Authorization')).toEqual(true);
expect(httpRequest.request.headers.get('Authorization')).toEqual('Token Teste');
});
这是我的服务:
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { InternalDataService } from '../internal-data/internal-data.service';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {
private token: string;
constructor(private internal: InternalDataService, private router: Router) {
this.internal.getAuthToken.subscribe( msg => this.token = msg );
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(request.url.match('.*\/auth$') == null) { // ignora se for url de autenticacao
request = request.clone({
setHeaders: {
Authorization: `Token ${this.token}`
}
});
}
return next.handle(request).pipe(
catchError((error: HttpErrorResponse) => {
if(error.status == 401 || error.status == 403){
this.internal.setIsAuth(false);
this.router.navigateByUrl('/login');
}
return throwError(error);
})
);
}
}
有人能看到我在做什么吗?