Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何提高茉莉花的覆盖率_Angular_Typescript_Jasmine_Karma Jasmine - Fatal编程技术网

Angular 如何提高茉莉花的覆盖率

Angular 如何提高茉莉花的覆盖率,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我对茉莉花和因果报应一无所知。组件法在提高覆盖率方面面临的问题 如何增加订阅块中if-else条件的覆盖率 我在app.component.ts文件中调用了a服务方法。我必须验证响应并根据结果显示图像,但覆盖率没有得到覆盖 应用程序组件.ts export class AppComponent { title = 'bank'; flag: string; image: string; private regex: RegExp = new RegExp(/-(.*?)-/);

我对茉莉花和因果报应一无所知。组件法在提高覆盖率方面面临的问题

如何增加订阅块中if-else条件的覆盖率

我在app.component.ts文件中调用了a服务方法。我必须验证响应并根据结果显示图像,但覆盖率没有得到覆盖

应用程序组件.ts

export class AppComponent {
  title = 'bank';
  flag: string;
  image: string;

  private regex: RegExp = new RegExp(/-(.*?)-/);

  constructor(private service : FlagService) {}

  ngOnit() {
    this.service.getFlag().subscribe(
      (res) => {
        this.flag = res.FlagModel[0].code.match(this.regex)[1];

        if(this.flag === 'dev') {
          this.image = '../assessts/icons/dev.png'
        } else if(this.flag === 'qa') {
          this.image = '../assessts/icons/qa.png'
        } else if(this.flag === 'prod') {
          this.image = '../assessts/icons/prod.png'
        } 
      }, (error) =>{
        console.log(error);
       });

  }
}
describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let flagServiceTest: FlagService;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
      providers: [
        HttpClient,
        HttpHandler,
        FlagService
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    flagServiceTest = TestBed.get(FlagService);
    fixture.detectChanges();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

  it(`should have as title 'bank'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('bank');
  });

  it('should call subscribe method', fakeAsync(() => {
    const flagSpy = spyOn(flagServiceTest,'getFlag').and.callThrough();
    fixture.detectChanges();
    component.ngOnit();
    tick();
    expect(flagServiceTest.getFlag()).toHaveBeenCalled();
    expect(flagSpy).toHaveBeenCalled();
    expect(flagSpy.calls.any()).toBeTruthy();
  }));
});
app.component.specs.ts

export class AppComponent {
  title = 'bank';
  flag: string;
  image: string;

  private regex: RegExp = new RegExp(/-(.*?)-/);

  constructor(private service : FlagService) {}

  ngOnit() {
    this.service.getFlag().subscribe(
      (res) => {
        this.flag = res.FlagModel[0].code.match(this.regex)[1];

        if(this.flag === 'dev') {
          this.image = '../assessts/icons/dev.png'
        } else if(this.flag === 'qa') {
          this.image = '../assessts/icons/qa.png'
        } else if(this.flag === 'prod') {
          this.image = '../assessts/icons/prod.png'
        } 
      }, (error) =>{
        console.log(error);
       });

  }
}
describe('AppComponent', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let flagServiceTest: FlagService;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
      providers: [
        HttpClient,
        HttpHandler,
        FlagService
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    flagServiceTest = TestBed.get(FlagService);
    fixture.detectChanges();
  });

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

  it(`should have as title 'bank'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('bank');
  });

  it('should call subscribe method', fakeAsync(() => {
    const flagSpy = spyOn(flagServiceTest,'getFlag').and.callThrough();
    fixture.detectChanges();
    component.ngOnit();
    tick();
    expect(flagServiceTest.getFlag()).toHaveBeenCalled();
    expect(flagSpy).toHaveBeenCalled();
    expect(flagSpy.calls.any()).toBeTruthy();
  }));
});
  • 您需要为每个if语句创建一个测试用例,并且每次都需要从您的服务中提供一个合适的提要,因此我建议您使用spies,而不是引用真正的FlagService
  • 创建一个全局fixture,这样您就不必为每个测试用例实例化它
  • 您不需要调用。detectChanges,因为您已经调用了ngOnInit。
    请参阅下面的项目,它涵盖了100%的appComponent。 ()。
      • 您需要为每个if语句创建一个测试用例,并且每次都需要从您的服务中提供一个合适的提要,因此我建议您使用spies,而不是引用真正的FlagService
      • 创建一个全局fixture,这样您就不必为每个测试用例实例化它
      • 您不需要调用。detectChanges,因为您已经调用了ngOnInit。
        请参阅下面的项目,它涵盖了100%的appComponent。 ()。

      您可以为FalgService创建一个模拟,对于每个if案例,您可以模拟退货。您可以帮助我如何模拟if else案例。您可以为FalgService创建一个模拟,对于每个if案例,您可以模拟返回。您可以帮助我如何模拟if else案例吗?我如何访问FlagModel,因为从FlagService observable返回的是FlagModel。您的可观察对象正在返回字符串。FlagModel
      export interface Flag{'code':string;'desc':string;}export interface FlagModel{'flagDescription':Flag[];'employeeFlag':string;'region':Flag;'user':string;}
      运行时,我正在获取类型脚本:无法读取未定义的属性“0”。我无法理解您的服务输出,您说您返回了一个FlagModel,但在subscribe中似乎没有。无论我以何种方式将新提交推送到git,它都会响应您在评论中描述的内容。我如何访问FlagModel,因为从FlagService observable返回的是FlagModel。您的可观察对象正在返回字符串。FlagModel
      export interface Flag{'code':string;'desc':string;}export interface FlagModel{'flagDescription':Flag[];'employeeFlag':string;'region':Flag;'user':string;}
      运行时,我正在获取类型脚本:无法读取未定义的属性“0”。我无法理解您的服务输出,您说您返回了一个FlagModel,但在subscribe中似乎没有。无论我以何种方式向git推送新的提交,它都会响应您在评论中描述的内容。