首页 > 解决方案 > 如何将内部功能测试为角度功能?

问题描述

我有一个问题要问你们。是任何方式来测试内部函数,我讲successCallback和failureCallback;因为当我检查代码覆盖率时,这些功能没有被覆盖。

onSubmitForm() {
    const name: string = this.newForm.controls.name.value;
    const successCallback = () => {
      this.popupService.popupSuccess();
    };
    const failureCallback = () => {
      this.popupService.popupFailure();
    };
      this.myservice.edit({ name, this.id }, successCallback, failureCallback);
  }

标签: angularunit-testingjasminekarma-jasmine

解决方案


我认为这样的事情可以奏效。

it('should call popupSuccess on success', () => {
  component.onSubmitForm();
  // get the 2nd argument
  const successCallback = mockMyService.edit.calls.mostRecent().args[1];
  successCallback();
  expect(mockPopupService.popupSuccess).toHaveBeenCalled();
});

it('should call popupFailure on failure', () => {
  component.onSubmitForm();
  // get the 3rd argument
  const failCallback = mockMyService.edit.calls.mostRecent().args[2];
  failCallback();
  expect(mockPopupService.popupFailure).toHaveBeenCalled();
});

推荐阅读