Angular单元测试案例 – 我们如何编写订阅和响应的单元测试案例

huangapple go评论67阅读模式
英文:

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;
         }
      },  
    });
}

运行测试用例后,显示以下部分未被覆盖,请检查以下图像:

Angular单元测试案例 – 我们如何编写订阅和响应的单元测试案例

英文:

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;
         }
      },  
    });
}

Angular单元测试案例 – 我们如何编写订阅和响应的单元测试案例

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);
}

});

huangapple
  • 本文由 发表于 2023年2月16日 13:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定