Angular 7 +茉莉花-奇怪的伊斯坦布尔覆盖率问题(获得97相反100%)

时间:2019-06-05 13:18:03

标签: angular testing jasmine

我正遇到茉莉花的有线问题

当我使用Istanbul打印承保范围时,我获得了97%的承保范围,即100。 if语句中的其中一行被标记为红色(表示未涵盖), 但是可以肯定的是,即使我对其进行调试,我也看到调试器进入了if部分。

怎么可能?

您可以在下面的代码中看到我的评论,以了解哪一行是“不知情的”

这是我的测试。

<div class="header">
  //I need to put the slider from the custom-component in here

</div>

<div class="content">
  //I need to put the my-layer-component from the custom-component in here
</div>

这是我与服务相关的代码:

describe('testService', () => {
  let testBed: TestBed;
  let test: TestService;
  let httpClientMock: jasmine.SpyObj<HttpClient>;
  let loggerMock: jasmine.SpyObj<Logger>;


  beforeEach( async() => {
    httpClientMock = jasmine.createSpyObj('httpClient', ['get']);
    loggerMock = jasmine.createSpyObj('Logger', [
      'debug',
      'error',
      'trace',
      'info',
      'build'
    ]);
    loggerMock.build.and.returnValue(loggerMock);
    testBed = await TestBed.configureTestingModule({
      imports: [],
      declarations: [],
      providers: [
        TestService,
        HttpClient,
        { provide: Logger, useValue: loggerMock },
        { provide: HttpClient, useValue: httpClientMock }
      ]
    }).compileComponents();

  });

  beforeEach(() => {
    service = TestBed.get(TestService);
  });

  it('should create', () => {
    expect(service).toBeTruthy();
  });

  describe('TestService', () => {

    const subject$ = new AsyncSubject<any>();

    let incommingTestServerData: ReturnType<
      typeof getTestServerData
    >;
    let expectedTestResult: Test;
    const defaultsTestValue = new Test();

    beforeEach(() => {
      incommingTestServerData = getTestServerData();

      expectedTestResult = plainToClass(Test, incommingTestServerData);
    });

    // this test should pass the validation
    it('should get test from server', async () => {
      httpClientMock.get.and.returnValue(subject$);

      service.loadTets();
      subject$.next(incommingTestServerData);
      subject$.complete();

      const testRes: Test = await new Promise((resolve, reject) => {
        service.get$.subscribe(value => {
          resolve(value);
        });
      });

      expect(defaultsTestValue).toEqual(testRes);
    });

it('should resolve default value on validation error', async () => {
      // given
      httpClientMock.get.and.returnValue(subject$);
      incommingTestServerData.value = '10' as any; // create error in validation

      // when
      service.loadTest();
      subject$.next(incommingTestServerData);
      subject$.complete();

      // then
      const TestRes: Test = await new Promise((resolve, reject) => {
        service.get$.subscribe(value => {
          resolve(value);
        });
      });

      expect(defaultsTestValue).toEqual(TestRes);
    });

1 个答案:

答案 0 :(得分:0)

通过在beforeEach()中创建asyncSubject新对象进行修复

subject = new AsyncSubjecy<any>()

通过这种方式,我确保每个测试都会获得一个新的主题,并且不会使用上一个主题