我正在尝试对(使用vue-test-utils
)一个组件进行单元测试,该组件具有这样beforeRouteUpdate
的组件导航保护:
<template>
...
</template>
<script>
export default {
computed: {
...
}
...
beforeRouteUpdate(to, from, next) {
// code to be tested
this.$store.dispatch('setActiveTask')
}
}
</script>
我确实在测试文件中渲染了组件,shallowMount
并模拟了$store
.
beforeEach(() => {
cmp = shallowMount(Task, {
mocks: {
$store: store,
$route: {
params: {
taskID: 12,
programID: 1
}
}
},
i18n: new VueI18N({
silentTranslationWarn: true
}),
stubs: {
'default-layout': EmptySlotComponent,
'nested-router': EmptySlotComponent,
RouterLink: RouterLinkStub
}
})
})
it('has beforeRouteUpdate hook', () => {
// how do i call beforeRouteUpdate now?
// cmp.vm.beforeRouteUpdate(...)
}
有人对此有什么想法吗?
更新:我用 Mocha + Chai 作为单元测试工具创建了一个最小的例子@vue/cli
,可以在这里找到:https ://github.com/BerniWittmann/vue-test-navigation-guard-reproduction