vue单元测试如何测试mapState【solved】

tolerious
  • 455

按照官网给出的用例,但是还是会报错,说组件内的mapState中的变量没有定义。
hello.vue

<template>
  <div class="hello">
    <p>{{submitLocationTotal}}</p>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {};
  },
  methods: {},
  beforeCreate() {},
  mounted() {},
  watch: {},
  computed: {
    ...mapState({
      submitLocationTotal: state => state.claim.submitLocationTotal
    })
  },
  components: {}
};
</script>

<style lang='less'>
</style>

hello.spec.js

// import Vue from 'vue';
import hello from '@/hello.vue';
import Vuex from 'vuex';
import {
    shallowMount,
    createLocalVue
} from '@vue/test-utils'
const localVue = createLocalVue()
localVue.use(Vuex)

describe('hello', () => {
    let store


    beforeEach(() => {
        store = new Vuex.Store({
            state: {
                claim:{submitLocationTotal: 123} //这里之前没有加上,所有找不到这个变量值。
            },
        })
    })
    it('test value...', () => {
        const wrapper = shallowMount(hello, {
            store,
            localVue
        })
    })
})

然后运行vue run test:unit,会得到如下报错:
image.png

回复
阅读 1.2k
2 个回答
✓ 已被采纳

同意楼上,少了一级 claim

你知道吗?

宣传栏