大家好,我目前正在测试我的Angular应用,但我不知道为什么在覆盖报告中,从未涵盖可观察变量订阅中的行:
这是我组件中的方法:
onUpdateLastReview(
id: number,
dtDebut: Date,
dtFin: Date,
montant: number,
nivInvalidite: NivInvalidite,
orderDisplay: number,
pensionpere: number,
code: string,
libelleLong: string,
indicateur: any,
): void {
this.modifyFormLastReview.markAllAsTouched();
if (this.modifyFormLastReview.invalid) {
this.submitLastReview = true;
return;
}
{
this.mypensionreviselast = new PensionInvalidite();
const myOrganisme = new OrganismeFinanceur();
myOrganisme.code = code;
myOrganisme.libelleLong = libelleLong;
this.mypensionreviselast.dtDebut = dtDebut;
this.mypensionreviselast.dtFin = dtFin;
this.mypensionreviselast.montant = montant;
this.mypensionreviselast.nivInvalidite = nivInvalidite;
this.mypensionreviselast.orderDisplay = orderDisplay;
this.mypensionreviselast.montant = montant;
this.mypensionreviselast.pensionpere = pensionpere;
this.mypensionreviselast.indicateur = indicateur;
this.mypensionreviselast.organisme = myOrganisme;
this.serviceHttp.updatePension(id, this.mypensionreviselast).subscribe(res => {
this.serviceHttp.getAllPensions().subscribe(resp => { //not covered from here
this.pensionsInvalidite = resp;
this.pensionsInvalidite.sort((a, b) => a.orderDisplay - b.orderDisplay);
this.dateError = false;
this.submitLastReview = false;
this.modifyFormLastReview.reset();
this.orgaSubmit = false;
this.orgaSubmitBis = false;
this.myDtFin = null; //to here
});
});
}
}
这就是我在spec.ts中处理的方式: 我想找到一个测试,该测试涵盖了组件中方法的所有行。我正在尝试监视服务的方法,并返回响应以与组件方法中的对象“ component.pensionsInvalidite”进行比较,但它似乎不起作用...
describe('TestComponent', () => {
let component: TabPensionInvaliditeComponent;
let fixture: ComponentFixture<TabPensionInvaliditeComponent>;
let componentHtmlElement: HTMLElement;
let componentDebugElement: DebugElement;
let servicePensions: ServicePensionHTTPService;
const fb: FormBuilder = new FormBuilder();
const pensionsInvalidite: PensionInvalidite[] = [
{
pensionpere: -1,
dtDebut: new Date('1996-11-11'),
dtFin: new Date('1996-11-12'),
montant: 23,
nivInvalidite: NivInvalidite.inv1,
indicateur: Indicateur.initiale,
organisme: {
code: 'AUTRE REG.',
libelleLong: 'AUTRE REGIME SPECIAL',
libelleCourt: null,
},
orderDisplay: 0,
id: 1,
},
];
const mockServicePensions = {
savePension: () => {
return of(pensionsInvalidite);
},
updatePension: () => {
return of(pensionsInvalidite);
},
deletePension: () => {
return of(pensionsInvalidite);
},
getPension: () => {
return of(pensionIvalidite);
},
getAllPensions: () => {
return of(pensionsInvalidite);
},
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
TabPensionInvaliditeComponent,
],
imports: [
HttpClientTestingModule
],
providers: [
{ provide: ServicePensionHTTPService, useValue: mockServicePensions },
],
}).compileComponents();
servicePensions = TestBed.get(ServicePensionHTTPService);
}));
beforeEach(() => {
(fixture = TestBed.createComponent(TabPensionInvaliditeComponent)),
(component = fixture.componentInstance);
componentDebugElement = fixture.debugElement;
componentHtmlElement = componentDebugElement.nativeElement;
fixture.detectChanges();
});
it('Test onUpdateLastReview method', async(() => { //here is my test
spyOn(component, 'onUpdateLastReview').and.callThrough();
const response: PensionInvalidite[] = pensionsInvalidite;
spyOn(servicePensions, 'getAllPensions').and.returnValues(of({ response }));
component.onUpdateLastReview(
1,
pensionIvalidite.dtDebut,
pensionIvalidite.dtFin,
pensionIvalidite.montant,
pensionIvalidite.nivInvalidite,
pensionIvalidite.orderDisplay,
pensionIvalidite.pensionpere,
pensionIvalidite.organisme.code,
pensionIvalidite.organisme.libelleLong,
pensionIvalidite.indicateur,
);
component.pensionsInvalidite = pensionsInvalidite;
expect(component.onUpdateLastReview).toHaveBeenCalled();
expect(component.pensionsInvalidite).toEqual(response);
}));
})