Angular 用茉莉花业力测试树枝

Angular 用茉莉花业力测试树枝,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我想知道我怎样才能用茉莉花/业力来测试一根树枝。 例如,我有一个简单的函数: loadData(){ if(this.faktor){ // here it should be true or false this.callMethod1(); }else{ this.callMethod2(); } } 我需要增加测试覆盖率,并且需要测试分支。我尝试了下面的示例,但它不起作用。我需要将this.factor.isExist()设置为tr

我想知道我怎样才能用茉莉花/业力来测试一根树枝。 例如,我有一个简单的函数:

  loadData(){
    if(this.faktor){ // here it should be true or false
      this.callMethod1();
    }else{
      this.callMethod2();
    }
  }
我需要增加测试覆盖率,并且需要测试分支。我尝试了下面的示例,但它不起作用。我需要将this.factor.isExist()设置为true。我怎么做

这是我的测试组件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ChartComponent } from './chart.component';

describe('ChartComponent', () => {
  let component: ChartComponent;
  let fixture: ComponentFixture<ChartComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ ChartComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ChartComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

  it('should call method1  if factor exist', () => {
    const spy =  spyOn(component, 'callMethod1');
    component.factor.isExist() as true;
    expect(spy).toHaveBeenCalled();
  })

  it('should call method2 if factor not exist', () =>{
    const spy =  spyOn(component, 'callMethod2');
    component.factor.isExist() as false;
    expect(spy).toHaveBeenCalled();
  })
});
从'@angular/core/testing'导入{async,ComponentFixture,TestBed};
从“./chart.component”导入{ChartComponent};
描述('ChartComponent',()=>{
let组件:chart组件;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[图表组件]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(ChartComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('如果因子存在,则应调用method1',()=>{
constspy=spyOn(组件“callMethod1”);
component.factor.isExist()为true;
期望(间谍)。已被调用();
})
它('如果因子不存在,则应调用method2',()=>{
constspy=spyOn(组件“callMethod2”);
component.factor.isExist()为false;
期望(间谍)。已被调用();
})
});

始终不可能使代码覆盖率达到100%,但您的情况非常简单,您可以覆盖此处显示的所有代码

it('should call method1  if factor exist', () => {
    const spy =  spyOn(component, 'callMethod1');
    component.factor = 'Your mock data goes here'
    component.loadData();       // should execute if part
    expect(spy).toHaveBeenCalled();
  })

  it('should call method2 if factor not exist', () =>{
    const spy =  spyOn(component, 'callMethod2');
    component.factor = null;    
    component.loadData();     // should execute else part
    expect(spy).toHaveBeenCalled();
  })

你这里有什么问题?您在哪里调用
loadData()
方法?这只是一个示例。我可以从Html文件或其他函数调用。你可以选择。我需要测试分支,if语句应该是test if is true和false。如何将this.riskfaktor设置为true和false?这是测试分支的最佳方法吗?您可以简单地为负条件设置
component.factor=null
,为正测试设置
component.factor=some data
。如果factor是函数中的const factor,它也会起作用吗?您可以为组件的常量变量设置一个时间值。它也可能来自测试规范。你有这样一个例子吗?你可能会根据你的需求提出另一个问题。这样你问的问题就更清楚了