vue-test-utils提供了setComputed方法,该方法可让您设置计算属性的状态。
import { mount } from '@vue/test-utils'
const wrapper = mount(Home)
wrapper.setComputed({loaded: true})
vue-test-utils版本1.1.0.beta对读取
setComputed() has been deprecated and will be removed in version 1.0.0. You can overwrite computed properties by passing a computed object in the mounting options
的setComputed方法抛出弃用警告mounting options in the docs不提及任何计算对象。我去了
const wrapper = mount(Home, { computed: {loaded: true} })
和
const wrapper = mount(Home, {context: { computed: {loaded: true} } })
但是那些炸毁了。
为vue-test-utils设置计算属性的方式是什么?
最佳答案
挂载组件时,您可以覆盖计算选项:
const wrapper = mount(Home, {
computed: {
loaded() {
return true
}
}
})
但是模拟计算很危险。您可能会将组件置于生产期间无法处于的状态。
关于unit-testing - 使用vue-test-utils为单元测试设置Vue计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50437699/