我如何测试 bootstrap-vue toasts

How do I test bootstrap-vue toasts

我被指示断言我们的错误已在 bootstrap-vue toasts 中浮出水面。我如何断言我的测试已经完成了敬酒。我希望模拟出“this.$bvToast.toast”,但到目前为止我一直找不到任何突出的东西。

我偶然发现了同样的问题,我解决它的方法是将 this.$bvToast.toast() 包裹在组件方法 showToast():

周围
// component

{
   ...
   showToast(title: string, content: string) {
      this.$bvToast.toast(
         content,
         {
            title,
            ...
         }
      }
  }
}

然后,在我的笑话文件中,我只使用间谍并确保参数是我期望的参数

// component.spec

const mySpy = jest.spyOn(myComponent, 'showToast');
...
your code here
...
expect(mySpy).toHaveBeenCalledWith(expectedTitle, expectedContent)

当然,这不会测试 bootstrap-vue toast 组件中的实际文本,但这也违背了单元测试的目的:您假设 toast 正常工作 :)