带有setTimeout测试用例的加载服务未包含在角度9中?

问题描述 投票:0回答:1

我如何测试下面的代码,所以代码覆盖率未涵盖它。用setTimeout测试用例加载服务未包含在角度9中。我已经筋疲力尽了,但似乎没有被涵盖。谁能帮我解决我的代码。

LoadingService

import { EventEmitter, Injectable, Output } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class LoadingService {
  loaderText: any;
  get showLoader(): any {
    return this._showLoader;
  }
  set showLoader(newData: any) {
    this._showLoader = newData;
  }

  constructor() {}
  @Output() userChangeEvent: EventEmitter<boolean> = new EventEmitter();

  _showLoader: boolean;
  setLoader(isShowLoader) {
    this.userChangeEvent.emit(isShowLoader);
  }

  getLoaderText(): any {
    return this.loaderText;
  }
  setLoaderText(text: any) {
    this.loaderText = text;
  }
}

App.ts

this.loadinSrvc.userChangeEvent.subscribe((user) =>
      setTimeout(() => {
        this.showLoader = {
          text: this.loadinSrvc.getLoaderText() || 'Please wait...',
          isLoader: user,
        };
      })
    );

这里是spec文件

it(`should have as showloader configured`, async(() => {
      const app = fixture.debugElement.componentInstance;
      expect(app.showLoader).toEqual({
        isLoader: false,
        text: 'Please wait...',
      });
    }));

angular typescript unit-testing jasmine karma-jasmine
1个回答
0
投票

您必须使用fakeAsync区域方法,并调用flush清空宏任务队列,直到其为空。很多花哨的单词,但基本上这应该可行:

it(`should have as showloader configured`, fakeAsync(() => {
  const app = fixture.debugElement.componentInstance;
  flush();

  expect(app.showLoader).toEqual({
   isLoader: false,
   text: 'Please wait...',
  });
}));
© www.soinside.com 2019 - 2024. All rights reserved.