【问题标题】:Ionic unit testing alertcontroller离子单元测试警报控制器
【发布时间】:2020-11-09 21:52:53
【问题描述】:

我正在为一个使用警报告诉用户它正在使用 nfc 的应用程序工作。我正在对这个应用程序进行单元测试,并在 alertcontroller.create 方法上放置一个间谍,如下所示:

alertController.create = jasmine.createSpy().and.resolveTo({
    present: jasmine.createSpy()
});

在单元测试中,我想检查是否使用正确的警报选项调用它,如下所示:

      expect(alertController.create).toHaveBeenCalledWith({
        header: 'NFC aan het lezen...',
        message: 'Hou de pas tegen de achterkant van de telefoon',
        buttons: [
          {
            text: 'Anuleren',
            role: 'cancel',
            handler: () => nfc.stopRead()
          }
        ]
      });

但是问题是它在运行测试时由于处理程序而出错。如何有效地测试是否使用正确的值调用了 alertcontroller.create 函数?目前测试出现以下错误:

  Expected $[0].buttons[0].handler = Function to equal Function.

我想检查提供的对象是否正确但函数没有检查。

【问题讨论】:

  • nfc 是什么?
  • @KiranMistry nfc 是控制我读取 nfc 卡的类,它有一个 stopRead 方法,可以取消监听器的订阅,因此它只会在弹出窗口存在时读取。
  • 好的...所以nfc.stopRead() 函数如果没有调用就不能正确调用然后试试这个{ nfc.stopRead() }
  • @KiranMistry 用于执行此测试,无论是否调用它都无关紧要。问题出在单元测试中,以期望处理程序与实际代码中的相同。这只是我的单元测试,它应该查看是否使用提供的值调用了 alertController create 函数。

标签: angular typescript ionic-framework jasmine


【解决方案1】:

我可以使用它

jasmine.any(Function)

所以代码如下:

      expect(alertController.create).toHaveBeenCalledWith({
        header: 'NFC aan het lezen...',
        message: 'Hou de pas tegen de achterkant van de telefoon',
        buttons: [
          {
            text: 'Anuleren',
            role: 'cancel',
            handler: jasmine.any(Function) as any
          }
        ]
      });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2017-06-26
    相关资源
    最近更新 更多