英文:
Angular Unit Test case - How can we write unit test case for Subscribe and response
问题
以下是我的方法,我是 Angular 单元测试的新手。
fetchControlDetailById(controlName) {
this.controlService.fetchControlDetailById(controlName).subscribe({
next: (res: any) => {
this.controlDetails = res;
this.portiqueArr = res.portiqueList;
this.controlTest = res.controlTest;
this.controlPresentBool = res.controlPresent;
this.testSource = res.controlHistoryDto;
// 显示和隐藏下拉菜单部分
this.showPortiqueEssaiBool = true;
if (res.controlPresent) {
this.showPortiqueEssaiBool = false;
}
},
});
}
运行测试用例后,显示以下部分未被覆盖,请检查以下图像:
英文:
below is my method and I'm new in writing unit test case in angular.
fetchControlDetailById(controlName) {
this.controlService.fetchControlDetailById(controlName).subscribe({
next: (res: any) => {
this.controlDetails = res;
this.portiqueArr = res.portiqueList;
this.controlTest = res.controlTest;
this.controlPresentBool = res.controlPresent;
this.testSource = res.controlHistoryDto;
// show and hide dropdown section
this.showPortiqueEssaiBool = true;
if (res.controlPresent) {
this.showPortiqueEssaiBool = false;
}
},
});
}
After running the test case - it is showing below part is not covered - pls check below image
答案1
得分: 1
请尝试这个:
it('should fetch control details by ID and update component properties', () => {
const controlName = 'my-control-id';
const mockResponse = {
portiqueList: [/*...*/],
controlTest: {/*...*/},
controlPresent: true,
controlHistoryDto: [/*...*/]
};
spy = spyOn(controlService, 'fetchControlDetailById').and.returnValue(of(mockResponse));
component.fetchControlDetailById(controlName);
expect(spy).toHaveBeenCalledWith(controlName);
expect(component.controlDetails).toEqual(mockResponse);
expect(component.portiqueArr).toEqual(mockResponse.portiqueList);
expect(component.controlTest).toEqual(mockResponse.controlTest);
expect(component.controlPresentBool).toBe(mockResponse.controlPresent);
expect(component.testSource).toEqual(mockResponse.controlHistoryDto);
expect(component.showPortiqueEssaiBool).toBe(true);
if (mockResponse.controlPresent) {
expect(component.showPortiqueEssaiBool).toBe(false);
}
});
英文:
Try this
it('should fetch control details by ID and update component properties', () => {
const controlName = 'my-control-id';
const mockResponse = {
portiqueList: [/*...*/],
controlTest: {/*...*/},
controlPresent: true,
controlHistoryDto: [/*...*/]
};
spy = spyOn(controlService, 'fetchControlDetailById').and.returnValue(of(mockResponse));
component.fetchControlDetailById(controlName);
expect(spy).toHaveBeenCalledWith(controlName);
expect(component.controlDetails).toEqual(mockResponse);
expect(component.portiqueArr).toEqual(mockResponse.portiqueList);
expect(component.controlTest).toEqual(mockResponse.controlTest);
expect(component.controlPresentBool).toBe(mockResponse.controlPresent);
expect(component.testSource).toEqual(mockResponse.controlHistoryDto);
expect(component.showPortiqueEssaiBool).toBe(true);
if (mockResponse.controlPresent) {
expect(component.showPortiqueEssaiBool).toBe(false);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论