unit-testing - 使用vue-test-utils为单元测试设置Vue计算属性

标签 unit-testing vuejs2 vue-test-utils

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/

相关文章:

javascript - 测试数组以包含具有特定键 : value 的对象

vuejs2 - 如何从Vue插件访问Vuex?

javascript - 使用 Jest 测试组件时如何定位选择器的第一个子级

javascript - 类型错误 : Cannot read property '$on' of undefined in q-ajax-bar

javascript - 如何为未定义的窗口对象编写测试用例 | Vue 测试工具 | Jest 框架

javascript - 如何对提供/注入(inject)的 VUE 组件进行单元测试?

Android Studio 单元测试 : read data (input) file

Django 测试用例数据库给出不一致的响应、缓存或事务罪魁祸首?

unit-testing - 如果您已经进行了功能测试,还需要进行单元和集成测试吗?

javascript - Vue.JS "TypeError: reverseMessage is not a function"