0

帮助我覆盖 IF/Else 覆盖范围和返回 HTTP 响应的方法以及构造函数以覆盖Jasmine 单元测试用例的路由器和 Appconfig 服务的分支覆盖范围

在此处输入图像描述

4

1 回答 1

0

您可以使用 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();
于 2017-12-11T10:51:25.533 回答