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

按照官网给出的用例,但是还是会报错,说组件内的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

阅读 4.3k
2 个回答

同意楼上,少了一级 claim

claim 哪里来的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题