[英]Vue.js Unit Test - How to check if component has a method?
I have a simple component with one method in it. 我有一个带有一种方法的简单组件。 I would like to check if a method in it exists.
我想检查其中是否存在方法。 I know that I can check, if a component contains a certain class or a string, but can't figure out how to check for a method's name.
我知道我可以检查组件是否包含某个类或字符串,但无法弄清楚如何检查方法的名称。 Couldn't find any simple explanation.
找不到任何简单的解释。
Component to test: 要测试的组件:
<template>
<div>
<a id="returnButton" href="#" @click="toMainList" class="d-flex align-items-center back-to-button">
<font-awesome-icon :icon="['fas', 'arrow-left']" class="fontBasicIcon back-to-button-icon" />
<div>Back</div>
</a>
</div>
</template>
<script>
import router from '@/router'
export default {
name: 'back-to-button',
data () {
return {
}
},
methods:{
toMainList(){
$router.push('/mainlist');
}
}
}
</script>
Assuming you use Jest and mount your component to test via shallowMount
or mount
, the wrapper returned gives you access to the Vue instance (including all it's methods) 假设您使用Jest并通过
shallowMount
或mount
来安装组件以进行测试,则返回的包装shallowMount
您可以访问Vue实例(包括其所有方法)
const wrapper = shallowMount(Foo)
let instance = wrapper.vm // <- the Vue instance
let myMethod = instance.myMethod // <- a method callend 'myMethod' on the Vue instance
if(myMethod != undefined) {
console.log("method 'myMethod' exists")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.