【发布时间】:2017-06-20 06:43:09
【问题描述】:
我想触发这个 catch 块并确认 handleError() 运行:
@Injectable()
export class JwtService {
private actionUrl: string;
private headers: Headers;
constructor(private http: Http, private configurationService: ConfigurationService, private _utilsSvc: UtilitiesService) {}
getToken() {
if (this.configurationService.config) {
return this.http.post(this.configurationService.config.jwtUrl, { "brand": "IAG" })
.map(this._utilsSvc.parseJson)
.catch(this.handleError);
}
}
handleError(error: any) {
let errorStatus = error.status;
return Observable.throw(errorStatus);
}
}
utilsSvc 类:
@Injectable()
export class UtilitiesService {
constructor(private http: Http) { }
fetchFile(url) {
return this.http.get(url)
.toPromise();
}
parseJson(response: Response) {
return response.json();
}
}
我正在尝试传递无效的 json 以导致错误被捕获:
it("Should handle error", () => {
let resOp = new ResponseOptions({
body: "invalid json"
});
mockHttpResponse = new Response(resOp);
mockBackend.connections.subscribe(connection => {
connection.mockRespond(mockHttpResponse);
});
spyOn(jwtService, 'handleError');
jwtService.getToken().subscribe((res) => {
expect(jwtService.handleError).toHaveBeenCalled();
});
});
我收到此错误:
TypeError: 无法读取未定义的属性“Symbol(Symbol.iterator)”
如何让catch块捕捉到错误?
这是我的整个测试文件:
/// <reference path="../../typings/globals/es6-shim/index.d.ts"/>
/// <reference path="../../typings/globals/jasmine/index.d.ts" />
import { inject, TestBed, ComponentFixture } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { Headers, HttpModule, BaseRequestOptions, XHRBackend, Response, Http, ResponseOptions } from "@angular/http";
import { MockBackend, MockConnection } from "@angular/http/testing";
import { Component, DebugElement } from "@angular/core";
import { By } from '@angular/platform-browser';
import "rxjs/add/operator/toPromise";
import { Observable } from "rxjs/Rx";
import "rxjs/add/operator/toPromise";
import { AuthenticationLibrary } from "../../app/services/authenticationLibrary.service";
import { ConfigurationService } from "../../app/services/configuration.service";
import { FrontEndLoggingService } from "../../app/services/frontEndLogging.service";
import { JwtService } from "../../app/services/jwt.service";
import { UtilitiesService } from "../../app/services/utilities.service";
describe("Authentication service", () => {
let mockBackend;
let jwtService;
let configSvc;
let http;
let feLogSvc;
let mockHttpResponse;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{ provide: XHRBackend, useClass: MockBackend },
ConfigurationService,
FrontEndLoggingService,
JwtService,
UtilitiesService
]
});
mockBackend = TestBed.get(XHRBackend);
http = new Http(mockBackend, new BaseRequestOptions);
feLogSvc = TestBed.get(FrontEndLoggingService);
configSvc = TestBed.get(ConfigurationService);
configSvc.config = {
enableFrontEndDebugLogs: true,
baseUrl: "www.someurl.com",
providerEndpoint: "someMachine",
categoriesEndpoint: "someCatEndpoint"
}
jwtService = new JwtService(
TestBed.get(Http),
configSvc,
TestBed.get(UtilitiesService)
)
});
it("Should get token", () => {
let resOp = new ResponseOptions({
body: {
"token": "Bearer ABC123",
"brand": "AMI"
}
})
mockHttpResponse = new Response(resOp)
mockBackend.connections.subscribe(connection => {
connection.mockRespond(mockHttpResponse);
});
jwtService.getToken().subscribe(response => {
expect(response.token).toBe("Bearer ABC123");
});
});
it("Should handle error", () => {
let resOp = new ResponseOptions({
body: "sdvhfujvn"
});
mockHttpResponse = new Response(resOp);
mockBackend.connections.subscribe(connection => {
connection.mockRespond(mockHttpResponse);
});
spyOn(jwtService, 'handleError');
jwtService.getToken().subscribe((res) => {
expect(jwtService.handleError).toHaveBeenCalled();
});
});
});
【问题讨论】:
-
你能展示一下你是如何构造jwtService的