【问题标题】:Vue.js unit test testing a component , got props type errorVue.js 单元测试测试组件,得到道具类型错误
【发布时间】:2019-02-07 16:48:35
【问题描述】:

我正在尝试测试音频播放器(开始、暂停、停止、静音、定位、下载事件)

export default {
  name: "audioPlayer",
  props: {
    file: {
      type: Object,
      default: null
    },
    autoPlay: {
      type: Boolean,
      default: false
    },

在我的测试规范中,我将文件属性设置为以下

  const file = new File(["../../src/assets/audio/ultimo_desejo.mp3"], "ultimo_desejo", { type: "audio/mp3"});
  const ended = jest.fn();

但是当我运行测试时,我得到了一个错误:

console.error node_modules/vue/dist/vue.runtime.common.js:589 [Vue 警告]:无效的道具:道具“文件”的类型检查失败。预期对象,得到文件。

found in

---> <AudioPlayer>
       <Root>

我应该如何设置我的文件属性?

感谢反馈

【问题讨论】:

    标签: unit-testing vue.js


    【解决方案1】:

    file 属性的type 字段更改为File

    props: {
      file: {
        type: File,
        default: null
      },
    }
    

    然后,您可以在 file 属性中传递一个新的 File

    it('takes a file prop', () => {
      const file = new File(['foo'], 'foo.txt');
      const wrapper = shallowMount(MyFoo, {
        propsData: { file }
      });
      expect(wrapper.vm.file).toBeTruthy();
    })
    

    demo

    【讨论】:

    • 谢谢......但我正在与 url 斗争......我应该在哪里存储 foo 文件? (如果我将它存储在 src/assets/audio/foo.mp3 ...哪个网址?)
    猜你喜欢
    • 1970-01-01
    • 2017-05-21
    • 2020-06-09
    • 2019-04-26
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    相关资源
    最近更新 更多