Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing vue中包含jQuery和this.random()的单元测试方法_Unit Testing_Vue.js_Jestjs - Fatal编程技术网

Unit testing vue中包含jQuery和this.random()的单元测试方法

Unit testing vue中包含jQuery和this.random()的单元测试方法,unit-testing,vue.js,jestjs,Unit Testing,Vue.js,Jestjs,我不熟悉单元测试,我想用一个方法测试一个组件,在这个方法中我使用this.random函数和jQuery with Jest 我想测试是否调用了openPost方法 当我得到一个测试时,它抛出错误this.$不是函数和this.random不是函数 我如何写测试 组成部分: <template> <div @click="openPost('note')" class="ml-3 text-center pointer noteBtn"> <i

我不熟悉单元测试,我想用一个方法测试一个组件,在这个方法中我使用
this.random
函数和jQuery with Jest

我想测试是否调用了openPost方法

当我得到一个测试时,它抛出错误
this.$不是函数
this.random不是函数

我如何写测试

组成部分:

<template>
    <div @click="openPost('note')" class="ml-3 text-center pointer noteBtn">
      <i class="fal color-9e fa-handshake"></i>
      <div class=" fs-14 color-50">metting</div>
    </div>
</template>

export default {
  data() {
    return {
      addNewKey: '',
      addNewType: '',
      showModal: false,
      showNegoModal: false,
      data: {}
    }
  },
  methods: {
    openPost(type, data) {
      this.addNewType = type
      if (data) this.data = data
      else this.data = {}
      this.addNewKey = this.random()
      this.$nextTick(() => {
        if (type === 'call') {
          this.$('#createContact').modal('show')
        } else if (type === 'note') {
          this.$('#createNote').modal('show')
        } else if (type === 'negotiation') {
          if (data) {
            this.showNegoModal = true
            this.$nextTick(() => {
              this.$('#CompanyNegoModal2').modal('show')
            })
          } else this.createnegopopup.show = true
        }
      })
    }
}

测试组件不访问全局
$
,但访问需要模拟的实例属性:

wrapper = shallowMount(component, { mocks: { $ } })
jQuery可链接需要提供可链接的模拟:

modal = jest.fn();
$ = jest.fn().mockReturnValue({ modal });

测试组件不访问全局
$
,但访问需要模拟的实例属性:

wrapper = shallowMount(component, { mocks: { $ } })
jQuery可链接需要提供可链接的模拟:

modal = jest.fn();
$ = jest.fn().mockReturnValue({ modal });

我写这个:
test('calls openPost when noteBtn is click',()=>{const modal=jest.fn()const$=jest.fn().mockReturnValue({modal})wrapper=shallowMount(AddDetailToCompany,{mocks:{$})wrapper.find('.noteBtn').trigger('click')expect(wrapper.vm.random).not.toBe(未定义)expect(wrapper.vm.vm.$).not.toBe(未定义)}
但它抛出错误
this.$(…).modal不是一个函数
。不应该有
this.$(…).modal不是一个函数
错误如果你做了所有事情都正确,因为它是通过
mock提供的
,你可以用
expect(jest.isMockFunction(wrapper.vm.$().modal)).toBe(true)
。我刚刚检查了测试,它按预期工作。请提供一种方法,以便在错误持续出现时重现错误。错误可能指的是您没有模拟
$
的其他地方。顺便说一句,在原始测试中,如果您没有从测试中返回承诺,您将无法正确测试异步代码,请使用
async
函数and
wait wrapper.vm.$nextTick()
test
中。我写这篇文章:
test('noteBtn单击时调用openPost',()=>{const modal=jest.fn()const$=jest.fn().mockReturnValue({modal})wrapper=shallowMount(AddDetailToCompany,{mocks:{$}})wrapper.find('.noteBtn').trigger('click'))期望(wrapper.vm.random).not.toBe(未定义)expect(wrapper.vm.$).not.toBe(未定义)}
但它抛出错误
this.$(…).modal不是函数
。不应该有
this.$(…).modal不是一个函数
错误如果您所有操作都正确,因为它是通过
mock提供的
,您可以使用
expect(jest.isMockFunction(wrapper.vm.$().modal)).toBe(true)来测试它
。我刚刚检查了测试,它按预期工作。请提供一种方法,以便在错误持续出现时重现错误。错误可能指的是您没有模拟
$
的其他地方。顺便说一句,在原始测试中,如果您没有从测试中返回承诺,您将无法正确测试异步代码,请使用
async
函数and
wait wrapper.vm.$nextTick()
位于
test
中。