【发布时间】:2018-05-24 19:55:23
【问题描述】:
【问题讨论】:
标签: angular jasmine karma-jasmine angular4-router angular4-httpclient
【问题讨论】:
标签: angular jasmine karma-jasmine angular4-router angular4-httpclient
您可以使用 MockBackend 模拟 http 响应 参考:https://angular.io/api/http/testing/MockBackend
您可以轻松定义自己的响应并测试所有 if/else
// before each test
beforeEach(() => {
this.injector = ReflectiveInjector.resolveAndCreate([
{provide: ConnectionBackend, useClass: MockBackend},
{provide: RequestOptions, useClass: BaseRequestOptions},
Http,
// other dependencies like Router / AppConfig
]);
this.authenticationService = this.injector.get(AuthenticationService);
this.backend = this.injector.get(ConnectionBackend) as MockBackend;
this.backend.connections.subscribe((connection: any) => this.lastConnection = connection);
});
// in your test
const mockErrorResponse = {status: 200};
this.lastConnection.mockRespond(new Response(new ResponseOptions({body: JSON.stringify(mockErrorResponse)})));
tick();
【讨论】: