如何在Angular Jamsine中使用finalize关键字编写订阅方法的代码覆盖率?

时间:2019-03-21 10:53:15

标签: angular jasmine karma-jasmine code-coverage finalize

角度版本:“ @ angular / core”:“ 6.1.7”

茉莉花版本:“茉莉花芯”:“〜2.8.0”

文件名:app.component.ts (组件文件中的方法)

testFunction() {
this.testClassService.testServiceFunction(data).pipe(takeWhile(() => this.testModel.subscriberFlag))
  .subscribe((response: string) => {
    console.log('Service covered');
}); 
}

文件名:app.component.service.ts (此处是从上述组件调用的testServiceFunction)

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AppConfig } from '../../../../config/app.config';
import { AppLocalConfig } from '../../../../config/app.local.config';
import { environment } from '../../../../environments/environment';

@Injectable()
export class TestClassService {
  constructor(private readonly http: HttpClient) {
    const appConfig: any = (environment.envName === 'local') ? AppLocalConfig.getConfig() : AppConfig.getConfig();
    this.endpoints = appConfig.api;
  }
  testServiceFunction() {
    return true;
  }
}

文件名:上述testFunction方法的app.component.spec.ts 规范(工作正常)

it('spec to cover testFunction', () => {
  const response = 'success';
  spyOn(service, 'testServiceFunction').and.returnValue(of(response));
  component.testFunction();
});

如何使用finalize关键字编写以下订阅方法的规范??

testFunction() {
this.testClassService.testServiceFunction(data).pipe(takeWhile(() => this.testModel.subscriberFlag))
.pipe(finalize(() => {
    console.log('Finalize covered');
  }))
  .subscribe((response: string) => {
    console.log('Service covered');
  });
}

0 个答案:

没有答案