【问题标题】:Unhandled promise rejection: NotAllowedError: play() failed because the user didn't interact with the document first未处理的承诺拒绝:NotAllowedError: play() 失败,因为用户没有先与文档交互
【发布时间】:2021-10-25 08:54:25
【问题描述】:

我最近将我的应用程序从 angular 10 升级到 11,然后当我为应用程序运行单元测试时,我收到此错误并且测试终止。它大部分时间都会发生,但有时不会弹出错误并且测试照常运行。有关如何解决此问题的任何指针。

error

仅在运行单元测试而应用程序运行良好且没有错误时发生

【问题讨论】:

标签: angular typescript unit-testing karma-jasmine upgrade


【解决方案1】:

这个问题的主要问题是很难找出这个错误出现在哪个组件或服务中。此外,有时测试会在没有任何错误的情况下运行。

我对“play()”进行了全局搜索,这是问题的根本原因。

playAlertSound(){
const audio=new Audio()
audio.src= /* audio source link */
audio.load()
audio.play()
}

这是导致问题的代码 sn-p,特别是 audio.play()。我们可以在这里采取两种方法:

  1. 模拟上述函数调用
  2. 在 audio.play() 语句之前添加以下代码 sn-p(不确定此方法是否正确,因为我们正在更改可能导致应用程序错误的 typescript 文件)
audio.muted=true;
audio.autoplay=true;

我选择了第一种方法...前者简单易行。做这样的事情

spyOn(component,'playAlertSound').and.callFake(()=>{})

每次调用该函数时都对其进行模拟。

【讨论】:

    猜你喜欢
    • 2020-12-25
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多