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 如何模拟此操作。$refs.form.resetValidation()?_Unit Testing_Vue.js_Vuetify.js_Vue Test Utils - Fatal编程技术网

Unit testing 如何模拟此操作。$refs.form.resetValidation()?

Unit testing 如何模拟此操作。$refs.form.resetValidation()?,unit-testing,vue.js,vuetify.js,vue-test-utils,Unit Testing,Vue.js,Vuetify.js,Vue Test Utils,组件1.vue <template> <div> <v-form ref="form"> <v-text-field v-model="name" :counter="10" :rules="nameRules" label="Name"

组件1.vue

 <template>
    <div>
        <v-form
        ref="form">
            <v-text-field
                v-model="name"
                :counter="10"
                :rules="nameRules"
                label="Name"
                required
            ></v-text-field>
        </v-form>
    </div>
</template>

<script>
  export default {
    data: () => ({
      name: 'Test',
      nameRules: [
        v => !!v || 'Name is required',
        v => (v && v.length <= 10) || 'Name must be less than 10 characters',
      ]
    }),

    mounted() {
        this.$refs.form.resetValidation();
    }
  }
</script>
错误-类型错误:此.$refs.form.resetValidation不是函数


如果我使用mount而不是shallowMount,测试就会通过。但是我想知道如何模拟$ref.form.resetValidation。

您可以创建一个手动存根来替换将用于v-form的存根:

test('test1', () => {
  const wrapper = shallowMount(component1, {
    localVue,
    stubs: {
      VForm: {
        render: () => {},
        methods: {
          resetValidation: () => ()
        }
      }
   }
})
....

})

尝试将其包装到函数中
test('test1', () => {
  const wrapper = shallowMount(component1, {
    localVue,
    stubs: {
      VForm: {
        render: () => {},
        methods: {
          resetValidation: () => ()
        }
      }
   }
})
....