【问题标题】:Setting up Vue computed properties for unit tests with vue-test-utils使用 vue-test-utils 为单元测试设置 Vue 计算属性
【发布时间】:2018-10-30 10:51:13
【问题描述】:

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 设置计算属性的方法是什么?

【问题讨论】:

    标签: unit-testing vuejs2 vue-test-utils


    【解决方案1】:

    您可以在挂载组件时覆盖计算选项:

    const wrapper = mount(Home, {
      computed: {
        loaded() {
          return true
        }
      }
    })
    

    但是模拟计算是危险的。您可能会将您的组件置于生产期间无法进入的状态。

    【讨论】:

    • 模拟以您描述的方式计算是可以的(使用安装选项)。不过,最好避免使用setComputed
    猜你喜欢
    • 2019-10-24
    • 2021-09-11
    • 2019-07-16
    • 2021-04-03
    • 1970-01-01
    • 2018-03-15
    • 2019-04-17
    • 2020-12-01
    • 2018-09-28
    相关资源
    最近更新 更多