How can I effectively test a branch using Jasmin/Karma within Angular? Consider the following simple function:
loadData(){
if(this.faktor){ // here it should be true or false
this.callMethod1();
}else{
this.callMethod2();
}
}
I am looking to increase the test coverage and need to test the branches. I attempted the following example but encountered issues. How can I set this.factor.isExist() to true?
Below is my test component code:
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 exists', () => {
const spy = spyOn(component, 'callMethod1');
component.factor.isExist() as true;
expect(spy).toHaveBeenCalled();
})
it('should call method2 if factor does not exist', () =>{
const spy = spyOn(component, 'callMethod2');
component.factor.isExist() as false;
expect(spy).toHaveBeenCalled();
})
});