【问题标题】:Angular 2 Testing PrimeNG ConfirmationDialogAngular 2 测试 PrimeNG 确认对话框
【发布时间】:2017-12-07 13:48:06
【问题描述】:

我正在为以下代码编写 jasmine 单元测试:

this.confirmationService.confirm({
  message: 'Are you sure that you want to delete?',
  accept: () => {
   //some code to test
  }
});

如何在对话框中伪造点击是,以测试里面的代码 接受()函数?

【问题讨论】:

  • 没有什么说你不能使用 JQuery 来强制点击事件。

标签: angular testing jasmine primeng primeng-dialog


【解决方案1】:

在您的模板中:

<p-confirmDialog #confirmDialog header="Confirmation" icon="fa fa-question-circle" width="425"></p-confirmDialog>

在您的组件中:

@ViewChild('confirmDialog') confirmDialog: ConfirmDialog;

然后你就可以调用confirmDialog.accept()

【讨论】:

  • 您不应仅为测试添加其他组件属性。要正确测试它,您应该模拟确认函数并在内部调用假接受函数以模拟正确的行为
【解决方案2】:

你可以像这样创建间谍:

spyOn(confirmationService, 'confirm').and.callFake((params: Confirmation) => {
    params.accept();
});

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2017-10-19
    相关资源
    最近更新 更多